Java2实用教程(第二版)程序代码——第十二章 布局设计

  1//例子1
  2import java.applet.*;import java.awt.*;
  3public class Example12_1 extends Applet
  4{  public void init() 
  5   {  FlowLayout flow=new FlowLayout();
  6      flow.setAlignment(FlowLayout.LEFT);
  7      flow.setHgap(20);flow.setVgap(40);
  8      setLayout(flow);
  9      setBackground(Color.cyan);
 10      for(int i=1;i<=12;i++
 11         { add(new Button("i am "+i));
 12         }

 13   }

 14}

 15
 16//例子2
 17import java.awt.*;import java.applet.*;
 18public class Example12_2 extends Applet
 19{  Button button1,button2;
 20   Label label1,label2;
 21   TextArea text;
 22   public void init() 
 23   {  setLayout(new BorderLayout());  
 24      text=new TextArea(10,10);
 25      button1=new Button(""); 
 26      button2=new Button("西");
 27      label1=new Label("上北",Label.CENTER);
 28      label2=new Label("下南",Label.CENTER);    
 29      add(BorderLayout.NORTH,label1);
 30      add(label2,BorderLayout.SOUTH);
 31      add(button1,BorderLayout.EAST);
 32      add(BorderLayout.WEST,button2); 
 33      add(BorderLayout.CENTER,text);
 34   }
   
 35}

 36
 37//例子3
 38import java.applet.*;import java.awt.*;import java.awt.event.*;
 39class Mycanvas extends Canvas 
 40{  int x,y;
 41   Mycanvas(int a,int b)
 42   {  x=a;y=b;
 43      setSize(100,160);
 44   }

 45   public void paint(Graphics g) 
 46   {  g.setColor(Color.red);
 47      g.fillOval(50,50,4*x,4*y);//画实心椭圆
 48      g.drawString("我是第 "+x,10,150);
 49   }

 50}
 
 51public class Example12_3 extends Applet implements ActionListener
 52{  CardLayout mycard;
 53   Button button1,button2,button3;
 54   Mycanvas mycanvas[];
 55   Panel p; 
 56   public void init() 
 57   {  setLayout(new BorderLayout()); //小容器的布局是边界布局。
 58      mycard=new CardLayout();
 59      p=new Panel();
 60      p.setLayout(mycard);          //p的布局设置为mycard(卡片式布局)
 61      button1=new Button("first");
 62      button2=new Button("next");
 63      button3=new Button("last one"); 
 64      mycanvas=new Mycanvas[21]; 
 65      for(int i=1;i<=20;i++
 66         { mycanvas[i]=new Mycanvas(i,i);
 67           p.add("i am"+i,mycanvas[i]); 
 68         }

 69      button1.addActionListener(this);
 70      button2.addActionListener(this);
 71      button3.addActionListener(this);
 72      Panel p2=new Panel();
 73      p2.add(button1);p2.add(button2);p2.add(button3);
 74      add(p,BorderLayout.CENTER);add(p2,BorderLayout.SOUTH);
 75   }

 76   public void actionPerformed(ActionEvent e)
 77   {  if(e.getSource()==button1)
 78        {  mycard.first(p);
 79        }

 80      else if(e.getSource()==button2)
 81       {  mycard.next(p);
 82       }

 83      else if(e.getSource()==button3)
 84       {  mycard.last(p);
 85       }

 86   }

 87}

 88
 89//例子4
 90import java.applet.*; import java.awt.*;
 91import java.awt.event.*;
 92public class Example12_4 extends Applet 
 93{  GridLayout grid;
 94   public void init() 
 95   {  grid=new GridLayout(12,12); 
 96      setLayout(grid);
 97      Label label[][]=new Label[12][12];
 98      for(int i=0;i<12;i++)
 99        {  for(int j=0;j<12;j++)
100            {  label[i][j]=new Label();
101               if((i+j)%2==0)
102                 label[i][j].setBackground(Color.black);
103               else
104                 label[i][j].setBackground(Color.white);
105               add(label[i][j]); 
106            }

107         }

108   }

109}

110
111//例子5
112import javax.swing.*;import java.awt.*;import java.awt.event.*;
113import javax.swing.border.*;
114public class Example12_5 extends java.applet.Applet 
115{  Box baseBox ,boxH,boxV; 
116   public  void init() 
117   {  baseBox=Box.createHorizontalBox();
118      boxH=Box.createHorizontalBox();
119      boxV=Box.createVerticalBox();
120      for(int i=1;i<=5;i++)
121         { boxH.add(new JButton("按钮 "+i));
122           boxV.add(new JButton("按钮 "+i));
123         }

124      baseBox.add(boxH);baseBox.add(boxV);
125      add(baseBox); 
126   }

127}

128
129//例子6
130import javax.swing.*; import java.awt.*;
131import javax.swing.border.*;
132public class Example12_6 extends java.applet.Applet 
133{  Box baseBox ,boxV1,boxV2; 
134   public  void init()
135   {  boxV1=Box.createVerticalBox();
136      boxV1.add(new Label("输入您的姓名"));
137      boxV1.add(Box.createVerticalStrut(8));
138      boxV1.add(new Label("输入email"));
139      boxV1.add(Box.createVerticalStrut(8));
140      boxV1.add(new Label("输入您的职业"));
141      boxV2=Box.createVerticalBox();
142      boxV2.add(new TextField(16));
143      boxV2.add(Box.createVerticalStrut(8));
144      boxV2.add(new TextField(16));
145      boxV2.add(Box.createVerticalStrut(8));
146      boxV2.add(new TextField(16));
147      baseBox=Box.createHorizontalBox();
148      baseBox.add(boxV1);
149      baseBox.add(Box.createHorizontalStrut(10));
150      aseBox.add(boxV2);
151      add(baseBox); 
152   }

153}

154
155//例子7
156import javax.swing.*;import java.awt.*;
157import javax.swing.border.*;
158public class Example12_7 extends java.applet.Applet 
159{  Box baseBox ,boxV1,boxV2,boxV3; 
160   public void init()
161   {  boxV1=Box.createVerticalBox();
162      for(int i=1;i<=3;i++)
163       {  boxV1.add(new JButton("按钮"+i));
164       }

165      boxV1.add(Box.createVerticalGlue());
166      boxV2=Box.createVerticalBox();
167      boxV2.add(Box.createVerticalGlue());
168      for(int i=1;i<=8;i++)
169       {  boxV2.add(new JButton("按钮"+i));
170       }

171      boxV3=Box.createVerticalBox();
172      for(int i=1;i<=6;i++)
173       {  boxV3.add(new JButton("按钮"+i));
174          if(i==3)
175             boxV3.add(Box.createVerticalGlue());
176       }

177      baseBox=Box.createHorizontalBox();
178      baseBox.add(boxV1);baseBox.add(boxV2);
179      baseBox.add(boxV3); add(baseBox); 
180   }

181}

182
posted @ 2005-05-27 09:06  Rookie.Zhang  阅读(763)  评论(0编辑  收藏  举报