如果要看
currentCard
收益String
从cl.show(cardPanel, "" + (currentCard));
编辑(我尝试了您的代码示例)
1.你忘了初始化最重要的变量
private CardLayout cardLayout = new CardLayout();
2.那么SSCCE可能是
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class CardLayoutTest extends Jframe { private static final long serialVersionUID = 1L; private JPanel cardPanel, jp1, jp2, buttonPanel; private JLabel jl1, jl2; private JButton btn1, btn2; private CardLayout cardLayout = new CardLayout(); public CardLayoutTest() { setTitle("Test med CardLayout"); setSize(400, 300); cardPanel = new JPanel(); buttonPanel = new JPanel(); cardPanel.setLayout(cardLayout); jp1 = new JPanel(); jp2 = new JPanel(); jl1 = new JLabel("Card 1"); jl2 = new JLabel("Card 2"); jp1.add(jl1); jp2.add(jl2); cardPanel.add(jp1, "1"); cardPanel.add(jp2, "2"); btn1 = new JButton("Show Card 1"); btn1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cardLayout.show(cardPanel, "1"); } }); btn2 = new JButton("Show Card 2"); btn2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cardLayout.show(cardPanel, "2"); } }); buttonPanel.add(btn1); buttonPanel.add(btn2); add(cardPanel, BorderLayout.NORTH); add(buttonPanel, BorderLayout.SOUTH); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { CardLayoutTest frame = new CardLayoutTest(); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.setVisible(true); } }); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)