Java Reference
In-Depth Information
Listing 11-1. Working with the Box
import javax.swing.*;
import java.awt.*;
public class BoxSample {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame verticalFrame = new JFrame("Vertical");
verticalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box verticalBox = Box.createVerticalBox();
verticalBox.add(new JLabel("Top"));
verticalBox.add(new JTextField("Middle"));
verticalBox.add(new JButton("Bottom"));
verticalFrame.add(verticalBox, BorderLayout.CENTER);
verticalFrame.setSize(150, 150);
verticalFrame.setVisible(true);
JFrame horizontalFrame = new JFrame("Horizontal");
horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box horizontalBox = Box.createHorizontalBox();
horizontalBox.add(new JLabel("Left"));
horizontalBox.add(new JTextField("Middle"));
horizontalBox.add(new JButton("Right"));
horizontalFrame.add(horizontalBox, BorderLayout.CENTER);
horizontalFrame.setSize(150, 150);
horizontalFrame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Box Properties
As Table 11-1 shows, there are only two Box properties. Although the layout property inherits a
setLayout(LayoutManager) method from its parent Container class, if called on a Box object, the
class throws an AWTError . Once the BoxLayout manager is set during its construction, it can't be
changed, nor can its direction.
Table 11-1. Box Properties
Property Name
Data Type
Access
accessibleContext
AccessibleContext
Read-only
layout
LayoutManager
Write-only
Search WWH ::




Custom Search