日期:2014-05-20  浏览次数:20711 次

新手问题,不好意思,再次麻烦各位前辈了。
是一个关于CardLayout布局的问题,我写了个东西,应该是按鼠标后,调出嵌入在Frame中的其它Panel图层,不过我运行时,按下鼠标,没有反应,Frame里面始终显示第一个Panel图层,因此请教一下,我写的东西错在哪里,请各位前辈多多帮忙,不胜感激~~~~代码是:  

import   java.awt.*;  
import   java.awt.event.*;  
public   class   TestCardLayout  
{  
Frame   f;  
Panel   p1,p2,p3,p4,p5;  
Label   l1,l2,l3,l4,l5;  
CardLayout   myCard;  
public   static   void   main(String[]   args)  
{  
TestCardLayout   ct   =   new   TestCardLayout();  
ct.go();  
}  
public   void   go()  
{  
f   =   new   Frame( "CardLayout ");  
myCard   =   new   CardLayout();  
f.setLayout(myCard);  
//f.setLayout(new   CardLayout());  
p1   =   new   Panel();  
p2   =   new   Panel();  
p3   =   new   Panel();  
p4   =   new   Panel();  
p5   =   new   Panel();  
l1   =   new   Label( "第一章 ");  
p1.setBackground(Color.yellow);  
p1.add(l1);  
l2   =   new   Label( "第二章 ");  
p2.setBackground(Color.red);  
p2.add(l2);  
l3   =   new   Label( "第三章 ");  
p3.setBackground(Color.green);  
p3.add(l3);  
l4   =   new   Label( "第四章 ");  
p4.setBackground(Color.white);  
p4.add(l4);  
l5   =   new   Label( "第五章 ");  
p5.setBackground(Color.cyan);  
p5.add(l5);  
f.addMouseListener(new   MouseAdapter()  
{  
public   void   mousePressed(MouseEvent   e){}  
public   void   mouseReleased(MouseEvent   e){}  
});  
f.addMouseMotionListener(new   MouseMotionAdapter(){});  
f.add(p1, "First ");  
f.add(p2, "Second ");  
f.add(p3, "Third ");  
f.add(p4, "Fourth ");  
f.add(p5, "Fifth ");  
f.addWindowListener(new   WindowAdapter()  
{  
public   void   windowClosing(WindowEvent   e)  
{  
Frame   f   =   (Frame)(e.getSource());  
f.dispose();  
System.exit(0);  
}  
});  
//myCard.show(f, "First ");  
f.setSize(400,400);  
f.setVisible(true);  
}  
}  


------解决方案--------------------
package common;
import java.awt.*;
import java.awt.event.*;
public class TestCardLayout
{
Frame f;
Panel p1,p2,p3,p4,p5;
Label l1,l2,l3,l4,l5;
CardLayout myCard;
public static void main(String[] args)
{
TestCardLayout ct = new TestCardLayout();
ct.go();
}
public void go()
{
f = new Frame( "CardLayout ");
myCard = new CardLayout();
f.setLayout(myCard);
//f.setLayout(new CardLayout());
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();
l1 = new Label( "第一章 ");
p1.setBackground(Color.yellow);
p1.add(l1);
l2 = new Label( "第二章 ");
p2.setBackground(Color.red);
p2.add(l2);
l3 = new Label( "第三章 ");