標籤:java 布局
自己學習java中AWT開發表單程式中的幾種視窗組件,做一下筆記
FlowLayout:
import java.awt.*;public class Wintwo2 extends Frame{//定義一個文字框。TextArea a=new TextArea("請準確填寫資訊");//定義三個按鈕組件。Button b1=new Button("提交");Button b2=new Button("取消");Button b3=new Button("重設");Wintwo2(){//設定視窗名稱。this.setTitle("調查資訊卡");//設定布局管理器為FlowLayout。this.setLayout(new FlowLayout());//將按鈕組件放到視窗中this.add(a);this.add(b1);this.add(b2);this.add(b3);//設定視窗的位置和大小this.setBounds(100, 100, 450, 350);//設定視窗可見度this.setVisible(true);}public static void main(String[] args) {// TODO Auto-generated method stubnew Wintwo2();}}
BorderLayout:
import java.awt.*;import java.awt.peer.TextAreaPeer;public class Winthree1 extends Frame{//定義五個按鈕組件TextArea b1=new TextArea("中");TextArea b2=new TextArea("南");Button b3=new Button("提交");Button b4=new Button("取消");Button b5=new Button("重設");Winthree1(){//設定視窗名稱this.setTitle("利器BorderLayout");//設定布局管理器BorderLayoutthis.setLayout(new BorderLayout());//將按鈕組件放入視窗規定位置this.add(b1, BorderLayout.CENTER);this.add(b2, BorderLayout.NORTH);this.add(b3, BorderLayout.SOUTH);this.add(b4, BorderLayout.WEST);this.add(b5, BorderLayout.EAST);//設定視窗位置和大小this.setBounds(300, 200, 450, 350);//設定視窗可見度this.setVisible(true);}public static void main(String[] args) {// TODO Auto-generated method stubnew Winthree1();}}
GridLayout:
import java.awt.*; public class Winfour2 extends Frame{TextArea b1=new TextArea("甲");TextArea b2=new TextArea("乙");Button b3=new Button("丙");Button b4=new Button("丁");Button b5=new Button("戊");Winfour2() {this.setTitle("Girdlayout布局");this.setLayout(new GridLayout(2, 3));this.add(b1);this.add(b2);this.add(b3);this.add(b4);this.add(b5);this.setBounds(100, 100, 450, 450);this.setVisible(true);}public static void main(String[] args) {// TODO Auto-generated method stubnew Winfour2();}}
CardLayout:
import java.awt.*;import java.awt.event.*;public class Winfive1 extends Frame implements ActionListener{//定義一個面板Panel p=new Panel();//定義五個按鈕Button bf=new Button("第一個");Button bl=new Button("最後一個");Button bn=new Button("下一個");Button bp=new Button("上一個");Button bg=new Button("搜尋");//定義一個單行文字框TextField tf=new TextField();//設定布局管理器CardLayout cl=new CardLayout();Winfive1() {//設定視窗名稱this.setTitle("利用CardLayout布局組件");this.setBackground(Color.blue);this.setResizable(false);this.setLayout(null);this.add(p);//定義p面板為cardlayout布局管理器p.setLayout(cl);//為cardlayout布局管理器添加組件for(int i=1; i<=3; i++){Button btemp=new Button("布局按鈕"+i);p.add(btemp, " "+i);}//為每個組件設定大小位置並添加到容器中p.setBounds(10, 40, 100, 100);this.add(bf);bf.addActionListener(this);bf.setBounds(120, 40, 60, 20);this.add(bl);bl.addActionListener(this);bl.setBounds(120, 70, 60, 20);this.add(bn);bn.addActionListener(this);bn.setBounds(120, 100, 60, 20);this.add(bp);bp.addActionListener(this);bp.setBounds(120, 130, 60, 20);this.add(bg);bg.addActionListener(this);bg.setBounds(60, 160, 40, 20);this.add(tf);tf.setBounds(20, 160, 40, 20);//設定視窗的位置和大小this.setBounds(200, 200, 400, 380);//設定視窗的可見度this.setVisible(true);}//實現ActionListener介面中的actionPerformed方法public void actionPerformed(java.awt.event.ActionEvent e){//next方法if(e.getSource()==bn){cl.next(p);}//previous方法if(e.getSource()==bp){cl.previous(p);}//first方法if(e.getSource()==bf){cl.first(p);}//last方法if(e.getSource()==bl){cl.last(p);}//show方法if(e.getSource()==bg){cl.show(p, tf.getText().trim());tf.setText(" ");}}public static void main(String[] args) {// TODO Auto-generated method stubnew Winfive1();}}
Null:
import java.awt.*;import java.awt.Event.*;public class Winnull2 extends Frame{//定義三個按鈕Button b1=new Button("甲");Button b2=new Button("乙");Button b3=new Button("丙");public Winnull2() {//設定視窗名稱this.setTitle("null布局");//設定null布局管理器this.setLayout(null);//將組件添加到容器this.add(b1);this.add(b2);this.add(b3);//設定組件的大小和位置b1.setBounds(50, 50, 100, 50);b2.setBounds(50, 120, 100, 50);b3.setBounds(50, 190, 100, 50);//設定視窗位置和大小this.setBounds(100, 100, 450, 400);//設定視窗可見度this.setVisible(true);}public static void main(String[] args) {// TODO Auto-generated method stubnew Winnull2();}}
java 學習 —— AWT布局組件