在AWT中,有五种布局管理器分别是:
BorderLayout
FlowLayout
GridLayout
CardLayout
GridBagLayout
先看看代码
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class MyFrame implements WindowListener{
public static void main(String []args)
{
Frame f=new Frame("zhycheng");
f.setSize(300, 400);
f.setLocation(400, 300);
f.setBackground(Color.CYAN);
f.setLayout(new BorderLayout(10,10));
//f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//f.setLayout(new GridLayout(3,2,10,10));
Button btn=new Button("winsun");
f.add(btn,"Center");
Button btn1=new Button("north");
f.add(btn1,"North");
Button btn2=new Button("South");
f.add(btn2,"South");
Button btn3=new Button("West");
f.add(btn3,"West");
Button btn4=new Button("East");
f.add(btn4,"East");
f.setVisible(true);
f.addWindowListener(new MyFrame());
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
1.BorderLayout
BorderLayout将Frame分为5部分,Center,North,South,West,East。BorderLayout也是Frame的默认的布局。
f.setLayout(new BorderLayout(10,10));其中的两个10分别表示组件之间的水平间隙和垂直间隙
效果如图
2.FlowLayout
FlowLayout是流式布局,将组件依次摆放
f.setLayout(new FlowLayout(FlowLayout.RIGHT));中的参数是右对齐。当然,也有左对齐,默认居中
效果如下
3.GridLayout
GridLayout是网格布局,将容器分为网格。
f.setLayout(new GridLayout(3,2,10,10));将容器分为3行2列,格子之间的水平和垂直间隙都为10.
效果如下
4.CardLayout
CardLayout可以实现翻牌的效果。CardLayout对象的next()方法来实现代码有所改动,所以我将全部的代码都贴出来
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class MyFrame implements WindowListener{
public static void main(String []args)
{
final Frame f=new Frame("zhycheng");
f.setSize(300, 400);
f.setLocation(400, 300);
f.setBackground(Color.CYAN);
//f.setLayout(new BorderLayout(10,10));
//f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//f.setLayout(new GridLayout(3,2,10,10));
final CardLayout cl=new CardLayout();
f.setLayout(cl);
ActionListener al=new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
cl.next(f);
}};
Button btn=new Button("winsun");
btn.addActionListener(al);
f.add(btn,"Center");
Button btn1=new Button("north");
btn1.addActionListener(al);
f.add(btn1,"North");
Button btn2=new Button("South");
btn2.addActionListener(al);
f.add(btn2,"South");
Button btn3=new Button("West");
btn3.addActionListener(al);
f.add(btn3,"West");
Button btn4=new Button("East");
btn4.addActionListener(al);
f.add(btn4,"East");
f.setVisible(true);
f.addWindowListener(new MyFrame());
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
效果如图