AWT的布局管理器(只有例子)

五种布局管理器:

FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout


1、FlowLayout



package gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Ecample08 {
public static void main(String[] args) {
final Frame f=new Frame("FlowLayout");
f.setLayout(new FlowLayout(FlowLayout.CENTER,20,30));
f.setSize(200,300);
f.setLocation(300,200);
Button but=new Button("第一个按钮");
f.add(but);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
Window window=e.getWindow();
window.dispose();
}
});
but.addActionListener(new ActionListener() {
private int num=1;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
f.add(new Button("第"+ ++num+"个按钮"));
f.setVisible(true);
}
});
f.setVisible(true);
}
}




2、GridLayout


package gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Example10 {
public static void main(String[] args) {
final Frame f=new Frame("GridLayout");
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
Window window=e.getWindow();
window.dispose();
}
});
f.setLayout(new GridLayout(5,4));
f.setSize(300,300);
f.setLocation(400,300);
for(int i=0;i<6;i++){
f.add(new Button("but"+i));
}
f.setVisible(true);
}
}



这里遇到的一个问题是:当for循环添加组件时,如果循环的次数与添加的网格的数量不等的话,就不知道按照什么样的方式进行添加组件。比如说例子中的网格是(5,4),就是说5行4列,可是结果却是如图所示,目前没有想到到底是按照什么样的顺序来排的。


3、GridBagLayout:


package gui;
import java.awt.*;
class Layout extends Frame{
public Layout(String title) {
// TODO Auto-generated constructor stub
GridBagLayout layout=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
this.setLayout(layout);
c.fill=GridBagConstraints.BOTH;
c.weightx=1;
c.weighty=2;
this.addComponent("btn1",layout,c);
this.addComponent("btn2",layout,c);
this.addComponent("btn3",layout,c);
c.gridwidth=GridBagConstraints.REMAINDER;
this.addComponent("btn4", layout, c);


c.weightx=0;
c.weighty=0;
addComponent("btn5", layout, c);
c.gridwidth=1;

this.addComponent("btn6", layout, c);
c.gridwidth=GridBagConstraints.REMAINDER;
this.addComponent("btn7", layout, c);

c.gridheight=2;
c.gridwidth=1;
c.weightx=2;
c.weighty=2;
this.addComponent("btn8", layout, c);
c.gridwidth=GridBagConstraints.REMAINDER;
c.gridheight=1;
this.addComponent("btn9", layout, c);
this.addComponent("btn10", layout, c);


this.pack();
this.setVisible(true);

}
private void addComponent(String name, GridBagLayout layout, GridBagConstraints c) {
// TODO Auto-generated method stub
Button bt=new Button(name);
layout.setConstraints(bt, c);
this.add(bt);
}
}
public class Example11 {
public static void main(String[] args) {
new Layout("GridBagLayout");
}

}




4、CardLayout


package gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class Cardlayout extends Frame implements ActionListener{
Panel cardPanel=new Panel();
Panel controlpaPanel=new Panel();
Button nextButton,preButton;
CardLayout cardLayout=new CardLayout();
public Cardlayout() {
// TODO Auto-generated constructor stub
setSize(300,200);
setLocation(300,300);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
Cardlayout.this.dispose();
}
});
cardPanel.setLayout(cardLayout);
cardPanel.add(new Label("第一个界面",Label.CENTER));
cardPanel.add(new Label("第二个界面",Label.CENTER));
cardPanel.add(new Label("第三个界面",Label.CENTER));
nextButton=new Button("下一张卡片");
preButton=new Button("上一张卡片 ");
nextButton.addActionListener(this);
preButton.addActionListener(this);
controlpaPanel.add(nextButton);
controlpaPanel.add(preButton);
this.add(cardPanel, BorderLayout.CENTER);
this.add(controlpaPanel, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==nextButton){
cardLayout.next(cardPanel);
}
if(e.getSource()==preButton){
cardLayout.previous(cardPanel);
}
}
}
public class Example12 {
public static void main(String[] args) {
Cardlayout cardlayout=new Cardlayout();
}
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值