import java.awt.*; import java.awt.event.*; public class Menu1 implements ActionListener { Frame f; Panel p1,p2; Button b1,b2; Label lb1,lb2; public static void main(String arg[]) { (new Menu1()).display(); } public void display() { f=new Frame("My cardLayout"); f.setLayout(new CardLayout()); f.setVisible(true); f.setSize(260,260); f.setLocation(300,300); p1=new Panel(); p1.setBackground(Color.green); //p2=new Panel(); f.add(p1); //f.add(p2); p1.show(); //p2.setVisible(true); b1=new Button("b1"); b2=new Button("b2"); b1.show(); b2.show(); lb1=new Label("这是第一个panel",1); lb2=new Label("这是第二个panel",2); f.add(lb1); p1.add(b1); //p2.add(lb2); //p2.add(b2); p1.setVisible(true); b1.addActionListener(this); b2.addActionListener(this); f.addWindowListener(new Window1()); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { p2.setVisible(true); f.setBackground(Color.green); } if(e.getSource()==b2) { p1.setVisible(true); f.setBackground(Color.blue); } } } class Window1 extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } }
运行的时候,panel不能被加到frame上,还有其它错误,我该怎么办