Java Reference
In-Depth Information
The buttons look much better with raised edges. If you wanted them to appear sunken, you would specify
BevelBorder.LOWERED as the constructor argument. You might like to try out a SoftBevelBorder too.
All you need to do is use the class name, SoftBevelBorder , when creating the border.
Using a Border Layout Manager
The border layout manager is intended to place up to five components in a container. Possible positions
for these components are on any of the four borders of the container and in the center. Only one
component can be at each position. If you add a component at a position that is already occupied, the
previous component will be displaced. A border is selected by specifying a constraint that can be
NORTH , SOUTH , EAST , WEST , or CENTER . These are all final static constants defined in the
BorderLayout.class .
You can't specify the constraints in the BorderLayout constructor since a different constraint has to
be applied to each component. You specify the position of each component in a container when you
add it using the add() method. We can modify the earlier application example to add five buttons to
the content pane of the application window in a border layout:
Try It Out - Testing the BorderLayout Manager
Make the following changes to TryFlowLayout.java to try out the border layout manager and
exercise another border class:
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.BorderLayout;
import javax.swing.border.EtchedBorder;
public class TryBorderLayout {
// The window object
static JFrame aWindow = new JFrame("This is a Border Layout");
public static void main(String[] args) {
Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size
// Set the position to screen center & size to half screen size
aWindow.setBounds(wndSize.width/4, wndSize.height/4, // Position
wndSize.width/2, wndSize.height/2); // Size
aWindow.setDefaultCloseOperation(JFrame.EXIT _ ON _ CLOSE);
BorderLayout border = new BorderLayout(); // Create a layout manager
Container content = aWindow.getContentPane(); // Get the content pane
content.setLayout(border); // Set the container layout mgr
EtchedBorder edge = new EtchedBorder(EtchedBorder.RAISED); // Button border
Search WWH ::




Custom Search