Advertisement
Constant | Description |
---|---|
BorderLayout.NORTH | It is a narrow side at the top, within the container |
BorderLayout.SOUTH | It is a narrow side at the bottom, within the container |
BorderLayout.EAST | It is a narrow side at the far right, within the container. |
BorderLayout.WEST | It is a narrow side at the far right, within the container.. |
BorderLayout.CENTER | It is a large part at the center of container. |
Constructor | Description |
---|---|
public BorderLayout() | Creates a BorderLayout, with no space between the components |
public BorderLayout(int horizontalSpace, int verticalSpace) | Creates a Borderlayout, which lets us specify the horizontal and vertical space between the components. |
Methods | Description |
---|---|
public void add(Component comp, Object region) | This method adds the component, comp, to a region in container, where a region is either of the five BorderLayout constant values. |
public void setLayout(LayoutManager object) | This method sets the layout of the components in a container. |
Advertisement
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BorderLayoutEx
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});
}//Closing the main method
}//Closing the class A
class A
{
JTextArea textArea;
JButton button1, button2, button3, button4;
JFrame jf;
JPanel jp;
A()
{
jf = new JFrame("BorderLayout Example");
jp= new JPanel();
button1 = new JButton("North");
button2 = new JButton("South");
button3 = new JButton("East");
button4 = new JButton("West");
textArea = new JTextArea(4,4); //calling TextField(int row, int columns)
//Setting the positioning of the components in container, JPanel, to BorderLayout
jp.setLayout(new BorderLayout()); //
jp.add(button1, BorderLayout.NORTH); //Adding button1, to the NORTH, top side
jp.add(button2, BorderLayout.SOUTH); //Adding button2, to the SOUTH, bottom side
jp.add(button3, BorderLayout.EAST); //Adding button3, to the EAST, far right side
jp.add(button4, BorderLayout.WEST); //Adding button4, to the North, far left side
jp.add(textArea, BorderLayout.CENTER); //Adding textarea, to the CENTER
jf.add(jp);
jf.setSize(320,200);
jf.setVisible(true);
}
}
When you run the code, you are presented a window that contains the components such as four buttons and a blank textarea
positioned using BorderLayout manager. As you may see in the figure1, the center area covers the maximun space, while four buttons at the sides have a narrow width.
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement