如何在CardLayout中显示不同的卡片?

如何在CardLayout中显示不同的卡片?,第1张

如何在CardLayout中显示不同的卡片?
  • 我看不到Java7 show(容器父代,字符串名称)或Java6 show(容器父代,字符串名称)已过时

  • 如果要看

    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); }        });    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5600806.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存