import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLatoutFrame extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;
JButton btPrevious = new JButton ("前一张");
JButton btNext = new JButton ("前一张");
JPanel flowPanel = new JPanel(new FlowLayout());
JPanel cardPanel = new JPanel(new FlowLayout());
int currentIndex = 0;
@SuppressWarnings("deprecation")
public CardLayoutFrame (){
this.getContentPane().add(flowPanel,BorderLayout.SOUTH);
this.getContentPane().add(flowPanel,BorderLayout.CENTER);
cardPanel.add(getCard(1),"Card1");
cardPanel.add(getCard(2),"Card2");
flowPanel.add(btPrevious);
flowPanel.add(btNext);
ActionListener listener = new ActionListener (){
public void actionPerformed(ActionEvent e){
switchCard();
}
};
btPrevious.addActionListener (listener);
btNext.addActionListener (listener);
this.setSize(300,200);
this.setTitle("GardLayoutDemo");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
JPanel getCard(int index){
JPanel panel = new JPanel (new BorderLayout());
JLabel label = new JLabel ("<HTML><h1 style = color:red>"+"这是第"+index+"张卡片"+"<h1></HTML>");
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
return panel;
}
void switchCard(){
CardLayout cl = (CardLayout)cardPanel.getLayout();
if(currentIndex == 0){
currentIndex++;
cl.show(cardPanel,"card2");
}else{
currentIndex--;
cl.show(cardPanel,"card1");
}
}
public static void main(String[] args){
CardLatoutFrame frame = new CardLatoutFrame();
}
}
在eclipse中运行 没有任何结果,好像中间this.show()语句有问题,还有最后主程序没法实例化。
还有这句 private static final long serialVersionUID = 1L;  是什么意思,eclipse加的

解决方案 »

  1.   

    我运行了没问题,不知道这程序什么意思,
    是要实现点了上一张下一张显示不同的字还是干什么
    点的时候会抛个异常
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.FlowLayout cannot be cast to java.awt.CardLayoutthis.show()过时了,最好用setVisible(true)
    serialVersionUID 是实现序列化的一个标识,相当于序列号,不要也可以
      

  2.   

    private static final long serialVersionUID = 1L;序列化的时候有可能被用到。简单来说就是当你在PC1上把一个对象序列化到一个a.txt中,然后把a.txt拿到PC2上面,那么当我在PC2上面想要把对象从a.txt中读出来的话,PC2上面的类的serialVersionUID与PC1上的必须一致,否则会失败。就是一种安全考虑。
      

  3.   

    代码运行出界面应该没什么问题
    这里转型可能有问题,把LayoutManager强转成CardLayout了
    CardLayout cl = (CardLayout) cardPanel.getLayout();this.show(),这里show方法是过时的方法,但还是可以用的private static final long serialVersionUID = 1L; 这句是序列化用的
      

  4.   

    程序没反应是因为类名跟构造函数名不一致CardLatoutFrame  CardLayoutFrame随便改那一个,改成一样的。文件名也改成一样的。
      

  5.   

    谢谢楼上各位的解答  我已经解决了部分问题, 但是还是不行,好像是CardLayout cl = (CardLayout) cardPanel.getLayout(); 这句有问题,因为一点按钮就报错,请问这里应该怎么改?
      

  6.   

    是的   用按钮在两张卡片间切换   但是好像CardLayout cl = (CardLayout) cardPanel.getLayout();  有错
      

  7.   

      谢谢您这么仔细  我都没发现  呵呵   但是还有问题  好像是CardLayout cl = (CardLayout) cardPanel.getLayout();  这句
      

  8.   

    答:写程序太错心.改这一句:
    JPanel cardPanel = new JPanel(new FlowLayout()   ); 改为
    JPanel cardPanel = new JPanel(new CardLayout()    ); 
      

  9.   

    这个地方也写错了吧
    this.getContentPane().add(flowPanel,BorderLayout.SOUTH); 
    this.getContentPane().add(flowPanel,BorderLayout.CENTER); 
    把同一个panel加到south又加到center?

    this.getContentPane().add(flowPanel, BorderLayout.SOUTH);
    this.getContentPane().add(cardPanel, BorderLayout.CENTER);
    还是怎么的
      

  10.   

    改好了,你试试import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;public class Test extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    JButton btPrevious = new JButton("前一张");
    JButton btNext = new JButton("前一张");
    JPanel flowPanel = new JPanel(new FlowLayout());
    JPanel cardPanel = new JPanel(new CardLayout() );
    int currentIndex = 0; public Test() {
    this.getContentPane().add(flowPanel, BorderLayout.SOUTH);
    this.getContentPane().add(cardPanel, BorderLayout.CENTER);
    cardPanel.add(getCard(1), "Card1");
    cardPanel.add(getCard(2), "Card2");
    flowPanel.add(btPrevious);
    flowPanel.add(btNext);
    ActionListener listener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    switchCard();
    }
    };
    btPrevious.addActionListener(listener);
    btNext.addActionListener(listener);
    this.setSize(300, 200);
    this.setTitle("GardLayoutDemo");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    } JPanel getCard(int index) {
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel(" <HTML> <h1 style = color:red>" + "这是第"
    + index + "张卡片" + " <h1> </HTML>");
    label.setHorizontalAlignment(JLabel.CENTER); panel.add(label);
    return panel;
    } void switchCard() {
    CardLayout cl = (CardLayout) cardPanel.getLayout();
    if (currentIndex == 0) {
    currentIndex++;
    cl.show(cardPanel, "Card2");
    } else {
    currentIndex--;
    cl.show(cardPanel, "Card1");
    }
    } public static void main(String[] args) {
    Test frame = new Test();
    }}顺便说一下,你创建的时候
    cardPanel.add(getCard(1), "Card1");
    cardPanel.add(getCard(2), "Card2");
    下面调用的时候用的是
    cl.show(cardPanel,"card2"); 
    cl.show(cardPanel,"card1"); 
    Card1和card1啊。。