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();
}
}









發佈了76 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章