Suppose that you want to place ten buttons and a text field on a frame. The buttons are placed in grid formation, but the text field is placed on a separate row. It is difficult to achieve the desired look by placing all the components in a single container. With Java GUI programming, you can divide a window into panels. Panels act as subcontainers to group user -interface components. You add the buttons in one panel, and then add the panel into the frame. The Swing version of panel is JPanel . You can use new JPanel() to create a panel with a default FlowLayout manager or new JPanel(LayoutManager) to create a panel with the specified layout manager. Use the add(Component) method to add a component to the panel. For example, the following code creates a panel and adds a button to it:
JPanel p = new JPanel(); p.add( new JButton( "OK" ));
Panels can be placed inside a frame or inside another panel. The following statement places panel p into frame f :
f.add(p);
Listing 12.6 gives an example that demonstrates using panels as subcontainers. The program creates a user interface for a microwave oven, as shown in Figure 12.12.
1 import java.awt.*; 2 import javax.swing.*; 3 4 public class TestPanels extends JFrame { 5 public TestPanels() { 6 // Create panel p1 for the buttons and set GridLayout 7 JPanel p1 = new JPanel(); 8 p1.setLayout( new GridLayout( 4 , 3 )); 9 10 // Add buttons to the panel 11 for ( int i = 1 ; i <= 9 ; i++) { 12 p1.add ( new JButton( "" + i)); 13 } 14 15 p1.add( new JButton( "" + )); 16 p1.add( new JButton( "Start" )); 17 p1.add( new JButton( "Stop" )); 18 19 // Create panel p2 to hold a text field and p1 20 JPanel p2 = new JPanel( new BorderLayout()); 21 p2.add ( new JTextField( "Time to be displayed here" ), 22 BorderLayout.NORTH); 23 p2.add (p1, BorderLayout.CENTER); 24 25 // add contents into the frame 26 add(p2, BorderLayout.EAST); 27 add( new JButton( "Food to be placed here" ), 28 BorderLayout.CENTER); 29 } 30 31 /** Main method */ 32 public static void main(String[] args) { 33 TestPanels frame = new TestPanels(); 34 frame.setTitle( "The Front View of a Microwave Oven" ); 35 frame.setLocationRelativeTo( null ); // Center the frame 36 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 37 frame.setSize( 400 , 250 ); 38 frame.setVisible( true ); 39 } 40 } |
The setLayout method is defined in java.awt.Container . Since JPanel is a subclass of Container , you can use setLayout to set a new layout manager in the panel (line 8). Lines 7 “8 can be replaced by JPanel p1 = new JPanel(new GridLayout(4, 3)) .
To achieve the desired layout, the program uses panel p1 of GridLayout to group the number buttons, the Stop button, and the Start button, and panel p2 of BorderLayout to hold a text field in the north and p1 in the center. The button representing the food is placed in the center of the frame, and p2 is placed in the east of the frame.
The statement (lines 21 “22)
p2.add( new JTextField( "Time to be displayed here" ), BorderLayout.NORTH);
creates an instance of JTextField and adds it to p2 . JTextField is a GUI component that can be used for user input as well as to display values.
Note
The Container class is the superclass for Swing GUI component classes, such as JButton . In theory, you could use the setLayout method to set the layout in a button and add components into a button, because all the public methods in the Container class are inherited into JButton , but for practical reasons you should not use buttons as containers. |