borderlayout in java

BorderLayout is a layout manager in Java that arranges components in a five-region layout: north, south, east, west, and center. Each component can occupy one or more of these regions, and the layout manager automatically resizes and positions them as the frame or container is resized.

Here's an example of how to use BorderLayout to create a simple GUI with a text area in the center and buttons at the top and bottom:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.BorderLayout;

public class BorderLayoutExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("BorderLayout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create a panel for the buttons
        JPanel buttonPanel = new JPanel();
        JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");
        buttonPanel.add(button1);
        buttonPanel.add(button2);

        // Create a text area for the center
        JTextArea textArea = new JTextArea();

        // Add the components to the frame using BorderLayout
        frame.add(buttonPanel, BorderLayout.NORTH);
        frame.add(textArea, BorderLayout.CENTER);
        frame.add(new JButton("OK"), BorderLayout.SOUTH);

        // Set the size of the frame and show it
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}
Sour‮gi.www:ec‬iftidea.com

In this example, we create a JFrame with a BorderLayout and add a JPanel with two buttons to the north region, a JTextArea to the center region, and a JButton to the south region. When the frame is resized, the layout manager automatically adjusts the size and position of the components to fit the available space.