1 / 24

Creating a Window

Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt. JFrame. The library class JFrame, defined in javax.swing is a more useful class for creating a window since it provides more facilities. Its superclasses are:

woody
Télécharger la présentation

Creating a Window

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating a Window

  2. A basic window in Java is represented by an object of the class Window in the package java.awt.

  3. JFrame • The library class JFrame, defined in javax.swing is a more useful class for creating a window since it provides more facilities. Its superclasses are: Object->Component->Container->Window-> Frame->JFrame

  4. JFrame object: • contains 200+ methods as it has 5 superclasses from which it inherits members. • Displays an application by creating an object of JFrame, calling a method for the object to set the size of the window, then calling a method to display the window. See TestWindow.java OR ShowFame.java

  5. DISPOSE_ON_CLOSE: frame and any components in it are destroyed. • DO_NOTHING_ON_CLOSE: makes the close operation for the frame window ineffective. • HIDE_ON_CLOSE: hides window by setting visible to false. This is the default action if you don’t call the setDefaultCloseOperation() method.

  6. Key Classes JFrame used as the basic Java application window an object of this class has a title bar and provision for adding a menu you add other components to it use subclasses to create a window class specific to your application add GUI components or draw in this window if required

  7. Key Classes (contd) JDialog used to define a dialog window that is used for entering data into a program in various ways

  8. Key Classes (contd) JApplet a program designed to run embedded in a web page. All applets have this class as a base you can draw in a JApplet object and also add menus and other components

  9. Key Classes (contd) JComponent subclasses of JComponent define menus, buttons, checkboxes, etc. these classes are used to create the GUI for your application

  10. Window Panes when you want to add GUI components or draw in a window displayed from a JFrame object, the components are added to a window pane. The same for an applet. window panes are objects that are containers which represent an area of a window. Most of the time a window pane called the content pane is used.

  11. The JFrame class defines methods to provide a reference to any of the panes. getContentPane() returns the content pane as type Container. This is the method used most frequently, since you normally add components to the content pane.

  12. Swing Components JButton class defines a regular pushbutton this component has a border of type BevelBorder added to it. JCheckBox class clicking on the checkbox changes it state from checked to unchecked

  13. JRadioButton class operate in a group where only one button can be in a pressed state at any one time. JMenuBar class defines a member usually found at the top of an application window JMenu class object represents a top-level menu item on a menubar JCheckBoxMenuItem class component is a menu item with a check that is ticked when the item is selected.

  14. JLabel class passive and does not react to input events so you can’t edit it. JTextField displays a single line of text, but is editable. JTextArea allows editing of multi-line text

  15. Adding Components to a Container The components stored in a container are recorded in an array within the container object. The array is increased in size when necessary to accommodate as many components as are present. To add a component to a Container use the add( ) method.

  16. The Size and Position of a Component Position is defined by x and y coordinates of type int Size is defined by width and height. Components have a preferred size which depends on the particular object eg. The preferred size of a JButton object that defines a button is the size that accommodates the label for the button.

  17. Methods to retrieve or alter size and position void setBounds(int x, int y, int width, int height - sets the position of the Component object to the coordinates(x,y), and the width and height of the object to the values defined by the 3rd and 4th arguments.

  18. Toolkit class contains information about the environment in which your application is running, including the screen size in pixels. Use the getToolkit() method to help set the size and position of a window on the screen.

  19. Visual Characteristics of a Component void setBackground(Color bColor) - sets background color to bColor. Color getBackground( ) - retrieves the current background color

  20. Container Layout Manager responsible for arranging components in a certain way in the container. Layout manager for a container determines the position and size of all the components in the container.

  21. FlowLayout - layout manager which places components in successive rows in a container, fitting as many on each row as possible, and starting on the next row as soon as a row is full. It is the default manager for JPanel objects. BorderLayout - places components against the 4 borders of the container and in the center. Default manager for JFrame, JDialog, JApplet objects.

  22. GridLayout - places components in the container in a rectangular grid with the number of rows and columns you specify. GridBagLayout - the most flexible and powerful of the layout managers, also more complicated to use. Components in the GBLayout need not be the same size as in Glayout, and the order in which the components are placed is arbitrary.

  23. Setting the layout manager To set the layout manager of a container you call the setLayout( ) method for the container. FlowLayout flow = new FlowLayout( ); aWindow.getContentPane( ).setLayout(flow); Note: we cannot add components directly to a JFrame object, we must add them to the content pane for the window. FlowLayout flow = new FlowLayout(FlowLayout.LEFT,20,30); //sets hgap & vgap

  24. See TryFlowLayout TryAppletLayout

More Related