Java: Border Layout Manager (swing)

Contributor Icon Contributed by William_Wilson Date Icon February 20, 2006  
Tag Icon Tagged: Java programming

how to use the Border Layout Manager


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

+ 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 2 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 straight forward, so the best way to learn it 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: william_a_wilson@hotmail.com
-William. ยง (marvin_gohan)

Previous recipe | Next recipe |
 
blog comments powered by Disqus