BorderLayout
(LayoutManagers):
LayoutManagers:
The LayoutManagers are used to arrange
components in a particular manner. LayoutManager is an interface that is
implemented by all the classes of layout managers. There are following classes
that represents the layout managers:
- java.awt.BorderLayout
- java.awt.FlowLayout
- java.awt.GridLayout
- java.awt.CardLayout
- java.awt.GridBagLayout
- javax.swing.BoxLayout
- javax.swing.GroupLayout
- javax.swing.ScrollPaneLayout
- javax.swing.SpringLayout
etc.
BorderLayout:
The BorderLayout is used to arrange the
components in five regions: north, south, east, west and center. Each region
(area) may contain one component only. It is the default layout of frame or
window. The BorderLayout provides five constants for each region:
- public
static final int NORTH
- public
static final int SOUTH
- public
static final int EAST
- public
static final int WEST
- public
static final int CENTER
Constructors of BorderLayout class:
- BorderLayout(): creates a
border layout but with no gaps between the components.
- JBorderLayout(int
hgap, int vgap): creates
a border layout with the given horizontal and vertical gaps between the
components.
Example of BorderLayout class:
1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class Border {
5. JFrame f;
6. Border(){
7. f=new JFrame();
8.
9. JButton b1=new JButton("NORTH");;
10. JButton b2=new JButton("SOUTH");;
11. JButton b3=new JButton("EAST");;
12. JButton b4=new JButton("WEST");;
13. JButton b5=new JButton("CENTER");;
14.
15. f.add(b1,BorderLayout.NORTH);
16. f.add(b2,BorderLayout.SOUTH);
17. f.add(b3,BorderLayout.EAST);
18. f.add(b4,BorderLayout.WEST);
19. f.add(b5,BorderLayout.CENTER);
20.
21. f.setSize(300,300);
22. f.setVisible(true);
23. }
24. public static void main(String[] args) {
25. new Border();
26. }
27. }
No comments:
Post a Comment