HomeComputer programmingJava programmingJava: Border Layout Manager (Swing)

Java: Border Layout Manager (Swing)

The following tutorial describes how to use the Border Layout Manager.


Layout Managers are used in the organization of Panels and Frames. The proper layout should be chosen to accommodate frame resizings and use.

The + signs represent empty space.

Border:
—————————
| Frame Title |▄|▐ |X|
—————————
|++++Top Item+++++ |
|++++++ _++++++++|
|Left Item|_|Right Item|
|+++++++++++++++ |
|++Bottom Item+++++|
—————————-
_
|_| can be a Center Item
*Items can be placed along the border/edges of a frame.

This time there is are only two constructors, the default and one in which the horizontal and vertical gap must be supplied.

public BorderLayout()
public BorderLayout(int hgap, int vgap)

This layout manager is fairly straightforward, so the best way to learn its use is with an example.
CODE Example:
import java.awt.*;
import javax.swing.*;
public class BorderLayoutManagerExample extends JFrame {

public BorderLayoutManagerExample (String title) {

super(title);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout(2,2));

// Add components using the layout manager
contentPane.add(BorderLayout.NORTH, new JButton(“North”));
contentPane.add(BorderLayout.SOUTH, new JButton(“South”));
contentPane.add(BorderLayout.EAST, new JButton(“East”));
contentPane.add(BorderLayout.WEST, new JButton(“West”));
contentPane.add(BorderLayout.CENTER, new JButton(“Center”));

}
public static void main(String args[]) {

BorderLayoutManagerExample frame = new BorderLayoutManagerExample(“Example”);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(true);

}

}

*NOTE: This frame’s buttons do not actually do anything. This is simply an example of using the Layout Manager.
-Try changing the fram.setResizable to true, and resize the frame.

Questions or Comments: [email protected]
-William. § (marvin_gohan)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!