Java Reference
In-Depth Information
Table 4-15.
JButton UIResource Elements (Continued)
Property String
Object Type
Button.textIconGap
Integer
Button.textShiftOffset
Integer
Button.toolBarBorderBackground
Color
ButtonUI
String
JPanel Class
The last of the basic Swing components is the
JPanel
component. The
JPanel
component
serves as both a general-purpose container object, replacing the AWT
Panel
container, and a
replacement for the
Canvas
component, for those times when you need a drawable Swing
component area.
Creating a JPanel
There are four constructors for
JPanel
:
public JPanel()
JPanel panel = new JPanel();
public JPanel(boolean isDoubleBuffered)
JPanel panel = new JPanel(false);
public JPanel(LayoutManager manager)
JPanel panel = new JPanel(new GridLayout(2,2));
public JPanel(LayoutManager manager, boolean isDoubleBuffered)
JPanel panel = new JPanel(new GridLayout(2,2), false);
With the constructors, you can either change the default layout manager from
FlowLayout
or change the default double buffering that is performed from
true
to
false
.
Using a JPanel
You can use
JPanel
as your general-purpose container or as a base class for a new component.
For the general-purpose container, the procedure is simple: Just create the panel, set its layout
manager if necessary, and add components using the
add()
method.
JPanel panel = new JPanel();
JButton okButton = new JButton("OK");
panel.add(okButton);
JButton cancelButton = new JButton("Cancel");
panel.add(cancelButton);