13. Graphics

 
[Page 403 ( continued )]

12.4. Frames

To create a user interface, you need to create either a frame or an applet to hold the user-interface components . Creating Java applets will be introduced in Chapter 16, "Applets and Multimedia." This section introduces frames.


[Page 404]

12.4.1. Creating a Frame

To create a frame, use the JFrame class, as shown in Figure 12.4.

Figure 12.4. Frame is a top-level container to hold GUI components.

The program in Listing 12.1 creates a frame.

Listing 12.1. MyFrame.java
 1   import   javax.swing.*;  2  3   public class   MyFrame {  4   public static void   main(String[] args) {  5      JFrame frame =   new   JFrame(   "MyFrame"   );  // Create a frame  6      frame.setSize(   400   ,   300   );  // Set the frame size  7      frame.setLocationRelativeTo(   null   );  // New since JDK 1.4  8      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  9      frame.setVisible(   true   );  // Display the frame  10    } 11  } 

The frame is not displayed until the frame.setVisible(true) method is invoked. frame.setSize(400, 300) specifies that the frame is 400 pixels wide and 300 pixels high. If the setSize method is not used, the frame will be sized to display just the title bar. Since the setSize and setVisible methods are both defined in the Component class, they are inherited by the JFrame class. Later you will see that these methods are also useful in many other subclasses of Component .

When you run the MyFrame program, a window will be displayed on-screen (see Figure 12.5(a)).

Figure 12.5. (a) The program creates and displays a frame with the title MyFrame . (b) An OK button is added to the frame.

Invoking setLocationRelativeTo(null) (line 7) centers the frame on the screen. Invoking setDefaultCloseOperation(JFrame.EXIT ON CLOSE) (line 8) tells the program to terminate when the frame is closed. If this statement is not used, the program does not terminate when the frame is closed. In that case, you have to stop the program by pressing Ctrl+C at the DOS prompt window in Windows or use the kill command to stop the process in Unix.


[Page 405]

Note

Recall that a pixel is the smallest unit of space for drawing on the screen. You can think of a pixel as a small rectangle and think of the screen as paved with pixels. The resolution specifies the number of pixels per square inch. The more pixels the screen has, the higher the screen's resolution. The higher the resolution, the more fine detail you can see.


12.4.2. Adding Components to a Frame

The frame shown in Figure 12.5(a) is empty. Using the add method, you can add components into the frame, as in Listing 12.2.

Listing 12.2. MyFrameWithComponents.java
 1   import   javax.swing.*;  2  3   public class   MyFrameWithComponents {  4   public static void   main(String[] args) {  5      JFrame frame =   new   JFrame(   "MyFrameWithComponents"   );  6  7  // Add a button into the frame  8  JButton jbtOK =   new   JButton(   "OK"   );  9  frame.add(jbtOK);  10 11      frame.setSize(   400   ,   300   ); 12      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 13      frame.setLocationRelativeTo(   null   );  // Center the frame  14      frame.setVisible(true); 15    } 16  } 

Each JFrame contains a content pane. A content pane is an instance of java.awt.Container . The GUI components such as buttons are placed in the content pane in a frame. Prior to JDK 1.5, you have to use the getContentPane method in the JFrame class to return the content pane of the frame, and then invoke the content pane's add method to place a component into the content pane. This was cumbersome. JDK 1.5 allows you to place components to the content pane by invoking a frame's add method. This new feature is called content pane delegation . Strictly speaking, a component is added into the content pane of a frame. But for simplicity we say that a component is added to a frame.

An object of JButton was created using new JButton("OK") , and this object was added to the content pane of the frame (line 9).

The add(Component comp) method defined in the Container class adds an instance of Component to the container. Since JButton is a subclass of Component , an instance of JButton is also an instance of Component . To remove a component from a container, use the remove method. The following statement removes the button from the container:

 container.remove(jbtOK); 

When you run the program MyFrameWithComponents , the window will be displayed as in Figure 12.5(b). The button is always centered in the frame and occupies the entire frame no matter how you resize it. This is because components are put in the frame by the content pane's layout manager, and the default layout manager for the content pane places the button in the center. In the next section, you will use several different layout managers to place components in other locations as desired.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net