您的位置:首页 > 其它

布局管理器-5

2011-02-10 08:52 169 查看
 1)布局管理器的程序实战
package testLayout;
import java.awt.*;
import java.awt.event.*;
public class TestCardout extends Frame {
    Panel plCenter=new Panel();
    CardLayout cl=new CardLayout();
    public TestCardout()//添加构造方法
    {
       Panel plWest=new Panel();//设置一个左侧面板
       plWest.setLayout(new GridLayout(3,1));//在左侧面板设置一个三行一列的表格
       Button btnPre=new Button("prev");//三个表格里添加三个按钮
       Button btnNext=new Button("next");
       Button btnThree=new Button("three");
       plWest.add(btnPre);
       plWest.add(btnNext);
       plWest.add(btnThree);
      
      
       plCenter.setLayout(cl);//在中间设置一个面板,并设置布局模式为卡片式
       plCenter.add(new Button("one"),"1");//添加5个匿名按钮
       plCenter.add(new Button("two"),"2");
       plCenter.add(new Button("three"),"3");
       plCenter.add(new Button("four"),"4");
       plCenter.add(new Button("five"),"5");
      
       class MyActionListener implements ActionListener
       {         //为左侧5个按钮添加监听器类
 
           public void actionPerformed(ActionEvent arg0) {
              if (arg0.getActionCommand().equals("prev")) {
                  cl.previous(plCenter);        //当按钮为prev时调用previous方法
              }else if (arg0.getActionCommand().equals("next")) {
                  cl.next(plCenter);//当按钮为next是调用卡片布局的next方法
              }else {
                  cl.show(plCenter, "3");//当按钮为three时,调用按标识显示的方法show
              }
             
           }
          
       }
       MyActionListener ma=new MyActionListener();//实例化监听器,并分别添加到三个按钮中
       btnPre.addActionListener(ma);
       btnNext.addActionListener(ma);
       btnThree.addActionListener(ma);
      
       add(plWest,"West");//把两个面板添加到主界面上
       add(plCenter,"Center");
 
       addWindowListener(new WindowAdapter(){//注册窗口适配器
           public void windowClosing(WindowEvent arg0) {//重写关闭窗口的方法
              dispose();     //释放该窗口
              System.exit(0);//停止程序的运行
           }
 
       });
    }
    public static void main(String[] args) {
       // TODO Auto-generated method stub
       System.out.println("starting TestStopWatch..");
       TestCardout t=new TestCardout();
       t.setSize(400, 400);//设置窗口的大小
       t.setTitle("布局管理器");
       t.setVisible(true);
    }
 
}
2)取消布局管理器
package testLayout;
import java.awt.*;
import java.awt.event.*;
public class TestLay extends Frame {
 
    public TestLay()//添加构造方法
    {
       setLayout(null);
       Button b1=new Button("第一个按钮");
       Button b2=new Button("第二个按钮");
       b1.setBounds(10, 100, 100, 100);
       b2.setBounds(30, 20, 30, 30);
      
       add(b1);
       add(b2);
      
       addWindowListener(new WindowAdapter(){//注册窗口适配器
           public void windowClosing(WindowEvent arg0) {//重写关闭窗口的方法
              dispose();     //释放该窗口
              System.exit(0);//停止程序的运行
           }
 
       });
    }
    public static void main(String[] args) {
       // TODO Auto-generated method stub
       System.out.println("starting TestStopWatch..");
       TestLay t=new TestLay();
       t.setSize(400, 400);//设置窗口的大小
       t.setTitle("TestMyButton");
       t.setVisible(true);
    }
 
}
 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  button class import string null