Java Reference
In-Depth Information
import java.awt.*;
import javax.swing.*;
public class BoxLayoutDemo extends JFrame
{
public BoxLayoutDemo(String title)
{
super(title);
//Create a Box with a vertical axis.
Box box = new Box(BoxLayout.Y_AXIS);
//Add some components to the Box.
box.add(new JButton(“OK”));
box.add(new JCheckBox(“Check here.”));
box.add(new JButton(“Click here to continue.”));
box.add(new JLabel(“Enter your name:”));
box.add(new JTextField());
//Add the Box to the content pane of this JFrame.
Container contentPane = this.getContentPane();
contentPane.add(box, BorderLayout.CENTER);
}
public static void main(String [] args)
{
JFrame f = new BoxLayoutDemo(“BoxLayoutDemo”);
f.pack();
f.setVisible(true);
}
}
In the output of the BoxLayoutDemo program shown in Figure 12.9,
notice that the width of the components was determined by the widest
component in the Box, which in this example is the JButton with the label
“Click here to continue.”
Nesting Panels
Now that we have discussed a variety of different layout managers, I want
to show you a fairly complex GUI that contains a variety of components. Fig-
ure 12.10 shows a GUI similar to a program I worked on about 5 years ago
when Swing first started becoming popular.
Search WWH ::




Custom Search