Tuesday, May 21, 2024
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.

| 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) {

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.setSize(500, 300);



*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)



Please enter your comment!
Please enter your name here

Most Popular


Recent Comments

error: Content is protected !!