JAVA BORDERLAYOUT
Java Swing Tutorial Explaining the BorderLayout . BorderLayout places swing components in the North, South, East, West and center of a container. All extra space is placed in the center area. You can add horizontal and vertical gaps between the areas.
Every content pane is initialized to use a BorderLayout. Components are added to a BorderLayout by using the add method. JFrame’s content pane default layout manager: BorderLayout. In BorderLayout, a component’s position is specified by a second argument to add.
JAVA BORDERLAYOUT Example
/*
* BorderLayoutDemo.java is a 1.4 application that requires no other files.
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class BorderLayoutDemo {
public static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container contentPane) {
// Use BorderLayout. Default empty constructor with no horizontal and vertical
// gaps
contentPane.setLayout(new BorderLayout(5,5));
if (!(contentPane.getLayout() instanceof BorderLayout)) {
contentPane.add(new JLabel("Container doesn't use BorderLayout!"));
return;
}
if (RIGHT_TO_LEFT) {
contentPane.setComponentOrientation(
java.awt.ComponentOrientation.RIGHT_TO_LEFT);
}
JButton jbnSampleButtons = new JButton("Button 1 (PAGE_START)");
contentPane.add(jbnSampleButtons, BorderLayout.PAGE_START);
jbnSampleButtons = new JButton("Button 2 (CENTER)");
jbnSampleButtons.setPreferredSize(new Dimension(200, 100));
contentPane.add(jbnSampleButtons, BorderLayout.CENTER);
jbnSampleButtons = new JButton("Button 3 (LINE_START)");
contentPane.add(jbnSampleButtons, BorderLayout.LINE_START);
jbnSampleButtons = new JButton("Long-Named Button 4 (PAGE_END)");
contentPane.add(jbnSampleButtons, BorderLayout.PAGE_END);
jbnSampleButtons = new JButton("5 (LINE_END)");
contentPane.add(jbnSampleButtons, BorderLayout.LINE_END);
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("BorderLayout Source Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane and add swing components to it
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
output
Download BorderLayout Source Code
No comments:
Post a Comment