BorderLayout
BorderLayout as Layout Manager in Java
Details: BorderLayout
A border layout is a type of user interface design where several components are arranged within a grid of cells with varying borders. The word layout is used to describe the process of arranging components within a container. In contrast, arranging components without using a container is referred to as structuring. A good way to understand the concept of border layouts is by comparing them to other types of layouts. For this reason, the following paragraphs will compare a traditional layout with one using a border layout and explain how they differ.
The main advantages of using a border layout include faster app load time, better performance, and easier development. They also reduce the chances of errors and simplify maintenance. This is because fewer elements must be managed by the compiler and runtime system. An app compiled with a strict compiler will run faster and display fewer errors if it uses a border layout over traditional layouts. Although traditional layouts have some advantages, in general, they are inferior to those of border layouts in most cases.
Guide: BorderLayout
In this article, we will explore BorderLayout as Layout Manager in Java. Take an example of a rectangular screen area divided into 5 points; North, East, West, South, and the Middle position. In other to achieve this, the following steps can be adhered to:
1 – Create a Java program with the file name, borderLayout.java in JCreator or NetBeans.
2 – Proceed to import the corresponding packages:
import java.awt.*; //used to access the BorderLayout class
import javax.swing.*; //used to access the JFrame and JButton class
3 – Now, initiate the variable in your main;
– variable frame for JFrame
– Insert 4-buttons; btnNorth, btnEast, btnWest and btnSouth as JButton.
JFrame frame = new JFrame("BorderLayout as Layout Manager");
JButton btnNorth, btnSouth, btnEast, btnWest;
btnNorth = new JButton("North");
btnSouth = new JButton("South");
btnEast = new JButton("East");
btnWest = new JButton("West");
4 – Use the setLayout method of JFrame to set the program layout to BorderLayout and insert the class.
We then add the 4 buttons inside the frame considering their different position in a BorderLayout.
frame.getContentPane().add(btnNorth, BorderLayout.NORTH);
frame.getContentPane().add(btnSouth, BorderLayout.SOUTH);
frame.getContentPane().add(btnEast, BorderLayout.EAST);
frame.getContentPane().add(btnWest, BorderLayout.WEST);
– The top position of the button will assume the BorderLayout.NORTH.
– BorderLayout.SOUTH as the bottom
– BorderLayout.WEST in the left
– BorderLayout.EAST in the right.
5 – Finally, we can proceed to set the size, visibility, and the close operation of the frame using the code underneath:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 100);
frame.setVisible(true);
Output: BorderLayout
Complete Source Code: BorderLayout as Layout Manager in Java
import java.awt.*; //used to access the BorderLayout class
import javax.swing.*; //used to access the JFrame and JButton class
public class borderLayout{
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout as Layout Manager");
JButton btnNorth, btnSouth, btnEast, btnWest;
btnNorth = new JButton("North");
btnSouth = new JButton("South");
btnEast = new JButton("East");
btnWest = new JButton("West");
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(btnNorth, BorderLayout.NORTH);
frame.getContentPane().add(btnSouth, BorderLayout.SOUTH);
frame.getContentPane().add(btnEast, BorderLayout.EAST);
frame.getContentPane().add(btnWest, BorderLayout.WEST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 100);
frame.setVisible(true);
}
}