Java Reference
In-Depth Information
Note how the size of each button is automatically adjusted to accommodate the button label. Of course,
the fonts in the example are both logical fonts, so they are available on every system. If you want to try
physical fonts, choose two from those that you have installed on your system.
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. Don't forget the
import statement for the class name; the border classes are defined in the javax.swing.border package.
Using a Border Layout Manager
The border layout manager is intended to place up to five components in a container. With this layout man-
ager you can place components on any of the four borders of the container and in the center. Only one com-
ponent can be at each position. If you add a component at a position that is already occupied, the previous
component is 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 because 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. You 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
This a variation on the TryFlowLayout.java example to try out the border layout manager and exercise
another border class:
import javax.swing.*;
import javax.swing.SwingUtilities;
import java.awt.*;
import javax.swing.border.EtchedBorder;
public class TryBorderLayout {
public static void createWindow(){
JFrame aWindow = new JFrame("This is the Window Title");
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.setSize(wndSize.width/2, wndSize.height/2);
// Set
window size
aWindow.setLocationRelativeTo(null);
// Center
Search WWH ::




Custom Search