我的CardLayout为什么不能使用啊?只要用到CardLayout,编译能通过,执行的时候就出现下面的异常?
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to lay
out: constraint must be a string
        at java.awt.CardLayout.addLayoutComponent(Unknown Source)
        at java.awt.Container.addImpl(Unknown Source)
        at java.awt.Container.add(Unknown Source)
        at test1.ShowFrame.init(FrameDemo.java:24)
        at test1.ShowFrame.<init>(FrameDemo.java:14)
        at test1.FrameDemo.main(FrameDemo.java:48)
___________________________________________________________________________
源程序:
package test1;import java.awt.*;
import java.awt.event.*;class ShowFrame
{
private Frame frame=new Frame();
private Button btn1=new Button("OK");
private Button btn2=new Button("Cancel"); ShowFrame(String s)
{
init(s);
} void init(String s)
{
frame.setTitle(s);
frame.setBackground(Color.black);
frame.setSize(640,480);
frame.setLocation(50,50);
frame.setLayout(new CardLayout());
frame.add(btn1);
frame.add(btn2);
//frame.addWindowListener(new FrameListener());
frame.addWindowListener(new WindowAdapter()
                                                         {
                                                         public void windowClosing(WindowEvent e){System.exit(0);}
 });//内部类 frame.setVisible(true);
}
}class FrameListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}public class FrameDemo
{
public static void main(String[] args)
{
ShowFrame sf=new ShowFrame("我的窗口!");
}
}

解决方案 »

  1.   

    没明白你想要什么效果。
    你意思是说要两个按钮只出现一个吗?
    我把思路给你:把两个frame放在contentPane上面,这样只会出现一个(因为是contentPane布局是cardLayout嘛)每个frame上加一个按钮,frame1: button1 ; frame2:button2对按钮做事件:如果点击了button1,则让frame1.setVisible(false),相应frame2.setVisible(true)如果点击了button2,则反过来就ok了总体思想就是cardLayout只能显示一个面板
      

  2.   

    /**
      *注意用于CardLayout的Add()方法,必须用标注将卡片名和卡片本身伟到布局管理器中.然后就可以用卡片名代替
      *索引号去引用卡片.. 这是并没有 setLayout(new CardLayout()); 而是创建了一个CardLayout对象 这里需要实际引用
      *这个对象,需要一个变量来保持对它的引用
      *理解一下  自己多写几个 布局方面的程序就OK咯!! 光看不写 过几天又忘没了
      */import java.awt.*;
    import java.awt.event.*;class ShowFrame implements ActionListener{
    private Frame frame=new Frame();
    private Button btn1=new Button("OK");
    private Button btn2=new Button("Cancel");
    private CardLayout layout;
    ShowFrame(String s){
    init(s);
    } void init(String s){
    layout=new CardLayout();
    frame.setTitle(s);
    frame.setBackground(Color.black);
    frame.setSize(640,480);
    frame.setLocation(50,50);
    frame.setLayout(layout);

    btn1.addActionListener(this);
    btn2.addActionListener(this);
    frame.add(btn1,"one");
    frame.add(btn2,"two"); //frame.addWindowListener(new FrameListener());
    frame.addWindowListener(new WindowAdapter(){
                         public void windowClosing(WindowEvent e){System.exit(0);}
    });//匿名内部类 frame.setVisible(true);
    }//end init

    public void actionPerformed(ActionEvent e){
    layout.next(frame);
    }

    }//end of ShowFrameclass FrameListener extends WindowAdapter{
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    }//end of FrameListenerpublic class FrameDemo{
    public static void main(String[] args){
    ShowFrame sf=new ShowFrame("我的窗口!");
    }
    }//end of FrameDemo
      

  3.   

    oo 错了 应该是这样:
    注意用于将卡片加入CardLayout的add()方法