绝对布局相对布局
In Java, Layout Managers is used for arranging the components in order. LayoutMananger is an interface which implements the classes of the layout manager.
在Java中,布局管理器用于按顺序排列组件。 LayoutMananger是实现布局管理器类的接口。
下面是一些用于表示布局管理器的类。 (Below are some of the class which are used for the representation of layout manager.)
1. java.awt.BorderLayout
1. java.awt.BorderLayout
2. java.awt.FlowLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
4. java.awt.CardLayout
5. javax.swing.BoxLayout
5. javax.swing.BoxLayout
边框布局 (Border Layout )
BorderLayout is used, when we want to arrange the components in five regions. The five regions can be north, south, east, west and the centre. There are 5 types of constructor in Border Layout. They are as following:
当我们要在五个区域中排列组件时,使用BorderLayout。 这五个区域可以是北部,南部,东部,西部和中部。 边框布局中有5种类型的构造函数。 它们如下:
1. public static final int NORTH
1. public static int NORTH
2. public static final int SOUTH
2. public static final int SOUTH
3. public static final int EAST
3. public static final int EAST
4. public static final int WEST
4. public static final int WEST
5. public static final int CENTER
5. public static final int CENTER
Example:
例:
import java.awt.*;
import javax.swing.*;
public class BorderDemo1
{
JFrame frame;
BorderDemo1()
{
frame=new JFrame();
JButton box1=new JButton("**NORTH**");;
JButton box2=new JButton("**SOUTH**");;
JButton box3=new JButton("**EAST**");;
JButton box4=new JButton("**WEST**");;
JButton box5=new JButton("**CENTER**");;
frame.add(box1,BorderLayout.NORTH);
frame.add(box2,BorderLayout.SOUTH);
frame.add(box3,BorderLayout.EAST);
frame.add(box4,BorderLayout.WEST);
frame.add(box5,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}
public static void main(String[] args)
{
new BorderDemo1();
}
}
网格布局 (Grid Layout)
Grid Layout is used, when we want to arrange the components in a rectangular grid.
当我们想要将组件排列在矩形网格中时,使用“网格布局”。
There are 3 types of constructor in Grid Layout. They are as following:
网格布局中有3种类型的构造函数。 它们如下:
1. GridLayout()
1. GridLayout()
2. GridLayout(int rows, int columns)
2. GridLayout(int行,int列)
3. GridLayout(int rows, int columns, inthgap, int vgap)
3. GridLayout(int行,int列,inthgap,int vgap)
Example:
例:
import java.awt.*;
import javax.swing.*;
public class GridDemo1{
JFrame frame1;
GridDemo1(){
frame1=new JFrame();
JButton box1=new JButton("*1*");
JButton box2=new JButton("*2*");
JButton box3=new JButton("*3*");
JButton box4=new JButton("*4*");
JButton box5=new JButton("*5*");
JButton box6=new JButton("*6*");
JButton box7=new JButton("*7*");
JButton box8=new JButton("*8*");
JButton box9=new JButton("*9*");
frame1.add(box1);
frame1.add(box2);
frame1.add(box3);
frame1.add(box4);
frame1.add(box5);
frame1.add(box6);
frame1.add(box7);
frame1.add(box8);
frame1.add(box9);
frame1.setLayout(new GridLayout(3,3));
frame1.setSize(500,500);
frame1.setVisible(true);
}
public static void main(String[] args) {
new GridDemo1();
}
}
流程布局 (Flow Layout)
Flow Layout is used, when we want to arrange the components in a sequence one after another.
当我们要一个接一个地排列组件时,使用Flow Layout。
There are 3 types of constructor in the Flow Layout. They are as following:
流布局中有3种类型的构造函数。 它们如下:
1. FlowLayout()
1. FlowLayout()
2. FlowLayout(int align)
2. FlowLayout(int align)
3. FlowLayout(int align, inthgap, intvgap)
3. FlowLayout(int align,inthgap,intvgap)
Example:
例:
import java.awt.*;
import javax.swing.*;
public class FlowDemo1{
JFrame frame1;
FlowDemo1(){
frame1=new JFrame();
JButton box1=new JButton("1");
JButton box2=new JButton("2");
JButton box3=new JButton("3");
JButton box4=new JButton("4");
JButton box5=new JButton("5");
JButton box6=new JButton("6");
JButton box7=new JButton("7");
JButton box8=new JButton("8");
JButton box9=new JButton("9");
JButton box10=new JButton("10");
frame1.add(box1);
frame1.add(box2);
frame1.add(box3);
frame1.add(box4);
frame1.add(box5);
frame1.add(box6);
frame1.add(box7);
frame1.add(box8);
frame1.add(box9);
frame1.add(box10);
frame1.setLayout(new FlowLayout(FlowLayout.LEFT));
frame1.setSize(400,400);
frame1.setVisible(true);
}
public static void main(String[] args) {
new FlowDemo1();
}
}
盒子布局 (Box Layout)
Box Layout is used, when we want to arrange the components vertically or horizontally.
当我们想要垂直或水平排列组件时,使用Box Layout。
BoxLayout(Container c, int axis)is the only constructor in the Box Layout
BoxLayout(Container c,int axis)是Box Layout中唯一的构造函数
Example:
例:
import java.awt.*;
import javax.swing.*;
public class BoxDemo1 extends Frame {
Button buttonBox[];
public BoxDemo1 ()
{
buttonBox = new Button [2];
for (inti = 0;i<2;i++)
{
buttonBox[i] = new Button ("** Button " + (i + 1)+" **");
add (buttonBox[i]);
}
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(500,500);
setVisible(true);
}
public static void main(String args[])
{
BoxDemo1 obj=new BoxDemo1();
}
}
Example:
例:
import java.awt.*;
import javax.swing.*;
public class BoxDemo1 extends Frame {
Button buttonBox[];
public BoxDemo1 ()
{
buttonBox = new Button [2];
for (inti = 0;i<2;i++)
{
buttonBox[i] = new Button ("** Button " + (i + 1)+" **");
add (buttonBox[i]);
}
setLayout (new BoxLayout (this, BoxLayout.X_AXIS));
setSize(500,500);
setVisible(true);
}
public static void main(String args[])
{
BoxDemo1 obj=new BoxDemo1();
}
}
卡布局 (Card Layout)
Card Layout is used, when we want to see only one component at a time.
当我们想一次只看到一个组件时,使用卡片布局。
There are 2 types of constructor in the Card Layout. They are as following:
卡布局中有两种构造函数。 它们如下:
1. CardLayout()
1. CardLayout()
2. CardLayout(inthgap, intvgap)
2. CardLayout(inthgap,intvgap)
Example:
例:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardDemo1 extends JFrame implements ActionListener
{
CardLayoutc_Card;
JButton box1,box2,box3;
Container d;
CardDemo1()
{
d=getContentPane();
c_Card=new CardLayout(40,30);
d.setLayout(c_Card);
box1=new JButton("studytonight_1");
box2=new JButton("studytonight_2");
box3=new JButton("studytonight_3");
box1.addActionListener(this);
box2.addActionListener(this);
box3.addActionListener(this);
d.add("P",box1);
d.add("Q",box2);
d.add("R",box3);
}
public void actionPerformed(ActionEvent e)
{
c_Card.next(d);
}
public static void main(String[] args)
{
CardDemo1 obj =new CardDemo1();
obj.setSize(500,500);
obj.setVisible(true);
obj.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
绝对布局相对布局