Border Layout

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

BORDER LAYOUT

Name : D . Yeshwanth Kumar


Roll No : 22B81A0564
Branch : CSE-A
Layout Manager
◦ Layout refers to the arrangement of components within the container.
◦ In another way, it could be said that layout is placing the components at a particular position
within the container.
◦ The task of laying out the controls is done automatically by the Layout Manager.
◦ The layout manager automatically positions all the components within the container.
◦ Even if you do not use the layout manager, the components are still positioned by the default
layout manager.
◦ It is possible to lay out the controls by hand, however, it becomes very difficult because of the
following two reasons.
Border Layout
◦ The class Border Layout arranges the components to fit in the five regions: east, west, north,
south, and center.
◦ Each region can contain only one component and each component in each region is identified by the
corresponding constant NORTH, SOUTH, EAST, WEST, and CENTER.
◦ Border Layout is the default layout for the content panes of JWindow and JFrame objects.
◦ Because each component is associated with a direction, BorderLayout can manage at most
five components; it squashes or stretches those components to fit its constraints.

Class Declaration :
public class BorderLayout extends Object implements LayoutManager2, Serializable
public SwingLayoutDemo(){
import java.awt.*; prepareGUI();
import java.awt.event.*; }

import javax.swing.*; public static void main(String[] args){


SwingLayoutDemo swingLayoutDemo = new
SwingLayoutDemo();
public class SwingLayoutDemo {
private JFrame mainFrame; swingLayoutDemo.showBorderLayoutDemo();
private JLabel headerLabel; }

private JLabel statusLabel; private void prepare GUI(){


mainFrame = new JFrame("Java SWING
private JPanel controlPanel;
Examples");
private JLabel msglabel;
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3,
1));
mainFrame.add(headerLabel);
headerLabel = new JLabel("",JLabel.CENTER
); mainFrame.add(controlPanel);

statusLabel = new mainFrame.add(statusLabel);


JLabel("",JLabel.CENTER); mainFrame.setVisible(true);
statusLabel.setSize(350,100); } private void showBorderLayoutDemo(){
headerLabel.setText("Layout in
mainFrame.addWindowListener(new action: BorderLayout");
WindowAdapter() {
public void JPanel panel = new JPanel();
windowClosing(WindowEvent windowEvent){
panel.setBackground(Color.darkGray);
System.exit(0);
panel.setSize(300,300);
}
BorderLayout layout = new
}); BorderLayout();
controlPanel = new JPanel(); layout.setHgap(10);
controlPanel.setLayout(new layout.setVgap(10);
FlowLayout());
panel.setLayout(layout);
panel.add(new
JButton("Center"),BorderLayout.CENTER);
panel.add(new JButton("Line
Start"),BorderLayout.LINE_START);
panel.add(new JButton("Line
End"),BorderLayout.LINE_END);
panel.add(new
JButton("East"),BorderLayout.EAST);
panel.add(new
JButton("West"),BorderLayout.WEST);
panel.add(new
JButton("North"),BorderLayout.NORTH);
panel.add(new
JButton("South"),BorderLayout.SOUTH);
controlPanel.add(panel);
mainFrame.setVisible(true); }}
THANK YOU

You might also like