import java.awt.*;
import java.awt.event.*;class CardStart
{
  public static void main(String[] args)
  {
    CardTest c=new CardTest("MyCardLayoutTest");
  }
}class CardTest extends Frame
{
  Panel t1=new Panel();
  Panel t2=new Panel();
  Panel t3=new Panel();
  Panel t4=new Panel();
  Panel p1=new Panel();
  Panel p2=new Panel();
  Button b1=new Button();
  Button b2=new Button();
  Button b3=new Button();
  Button b4=new Button();
  CardLayout cl=new CardLayout();
  Label l1=new Label("The Number 1 CardPage");
  Label l2=new Label("The Number 2 CardPage");
  Label l3=new Label("The Number 3 CardPage");
  Label l4=new Label("The Number 4 CardPage");
  public CardTest(String s)
  {
    super(s);
    MyWL mw=new MyWL(this);
    myML mm=new myML(this);    this.addWindowListener(mw);
    this.p2.addMouseListener(mm);    b1.setSize(30,5);
    this.setLayout(new BorderLayout());
    this.setSize(500,400);
    p2.setSize(500,200);    t1.add(l1);
    t2.add(l2);
    t3.add(l3);
    t4.add(l4);    p1.setLayout(cl);
    p1.add("a",t1);
    p1.add("b",t2);
    p1.add("c",t3);
    p1.add("d",t4); p2.setLayout(new FlowLayout());
    p2.add(b1);
    p2.add(b2);
    p2.add(b3);
    p2.add(b4);    p1.setBackground(Color.blue);
    p2.setBackground(Color.red);
    this.add(p2,BorderLayout.SOUTH);
    this.add(p1,BorderLayout.CENTER);
    this.setResizable(false);
    this.setVisible(true);
  }
    public void T1()
    {
     cl.show(p1,"a");
    }
   public void T2()
    {
     cl.show(p1,"b");
    }
   public void T3()
    {
     cl.show(p1,"c");
    }
    public void T4()
    {
     cl.show(p1,"d");
    }
}class MyWL extends WindowAdapter
{
  CardTest t;
  public MyWL(CardTest ct)
  {
    t=ct;
  }
  public void windowClosing(WindowEvent we)
  {
    t.dispose();
  }
}
class myML extends MouseAdapter
{
  CardTest t;
  public myML(CardTest ct)
  {
    t=ct;
  }
  public void mouseClicked(MouseEvent me)
  {
   if((Button)me.getSource()==t.b1)
    {
     t.T1();
    }
    else if((Button)me.getSource()==t.b2)
    {
     t.T2();
    }
    else if((Button)me.getSource()==t.b3)
    {
     t.T3();
    }
    else if((Button)me.getSource()==t.b4)
    {
     t.T4();
    }
  }
}整个程序很简单,就是想联系CARDLAYOUT和其他的,但是发现就算不管封装性,把他们的PRIVATE都去掉,现在还是点了BUTTON不给我翻页,郁闷死了,而且,我明明设定的是500,200给的下边的PANEL,但是,大家运行都能看到,比例就不是我设定的哪个嘛,而且怎么改都改不动,我的BUTTON的大小也改不动,不论是用SETBOUNDS还是SETSIZE,郁闷了

解决方案 »

  1.   

    呵呵,如果轻易让你setSize,布局管理器就失业了。
      

  2.   

    应该是这一行吧this.setLayout(new BorderLayout());
    换一种布局方式
      

  3.   

    import java.awt.*;
    import java.awt.event.*;class CardStart {
    public static void main(String[] args) {
    CardTest c = new CardTest("MyCardLayoutTest");
    }
    }class CardTest extends Frame {
    Panel t1 = new Panel(); Panel t2 = new Panel(); Panel t3 = new Panel(); Panel t4 = new Panel(); Panel p1 = new Panel(); Panel p2 = new Panel(); Button b1 = new Button("1"); Button b2 = new Button("2"); Button b3 = new Button("3"); Button b4 = new Button("4"); CardLayout cl = new CardLayout(); Label l1 = new Label("The Number 1 CardPage"); Label l2 = new Label("The Number 2 CardPage"); Label l3 = new Label("The Number 3 CardPage"); Label l4 = new Label("The Number 4 CardPage"); public CardTest(String s) {
    super(s);
    MyWL mw = new MyWL(this);
    myML mm = new myML(this); this.addWindowListener(mw);
    b1.addActionListener(mm);
    b2.addActionListener(mm);
    b3.addActionListener(mm);
    b4.addActionListener(mm); b1.setSize(30, 5);
    this.setLayout(new BorderLayout());
    this.setSize(500, 400);
    p2.setSize(500, 200); t1.add(l1);
    t2.add(l2);
    t3.add(l3);
    t4.add(l4); p1.setLayout(cl);
    p1.add("a", t1);
    p1.add("b", t2);
    p1.add("c", t3);
    p1.add("d", t4); p2.setLayout(new GridLayout(1, 4));
    p2.add(b1);
    p2.add(b2);
    p2.add(b3);
    p2.add(b4); p1.setBackground(Color.cyan);
    p2.setBackground(Color.white);
    this.add(p2, BorderLayout.SOUTH);
    this.add(p1, BorderLayout.CENTER);
    this.setResizable(false);
    this.setVisible(true);
    } public void T1() {
    cl.show(p1, "a");
    } public void T2() {
    cl.show(p1, "b");
    } public void T3() {
    cl.show(p1, "c");
    } public void T4() {
    cl.show(p1, "d");
    }
    }class MyWL extends WindowAdapter {
    CardTest t; public MyWL(CardTest ct) {
    t = ct;
    } public void windowClosing(WindowEvent we) {
    t.dispose();
    }
    }class myML implements ActionListener {
    CardTest t; public myML(CardTest ct) {
    t = ct;
    } public void actionPerformed(ActionEvent me) {
    if (me.getSource() == t.b1) {
    System.out.println("1");
    t.T1();
    } else if (me.getSource() == t.b2) {
    System.out.println("2");
    t.T2();
    } else if (me.getSource() == t.b3) {
    System.out.println("3");
    t.T3();
    } else if (me.getSource() == t.b4) {
    System.out.println("4");
    t.T4();
    }
    }
    }
      

  4.   

    晕,半天是忘了给BUTTON添加MOUSELISTENER,谢了,但是关于使用P2使用FLOWLAYOUT为什么BUTTON大小和位置不能改变等这些问题还是不对