如何在 CardLayout 中显示不同的卡片? [英] How to show different cards in a CardLayout?

查看:23
本文介绍了如何在 CardLayout 中显示不同的卡片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了一个使用此代码的代码示例:

I looked at a code example that used this code:

cl.show(cardPanel, "" + (currentCard));

但是当我使用 show 时,我在 Eclipse 中收到一条消息说它已被弃用,我想知道当我单击按钮时是否有另一种方法可以在 CardLayout 中显示不同的卡片?下面是我的 CardLayout 类的代码.如果代码的某些部分是不好的做法,也欢迎提出建议.谢谢!

But when I use show I get a message in Eclipse that it's deprecated and I wonder if there is another way to show the different cards in the CardLayout when I click on the buttons? Below is the code for my CardLayout class. Suggestions are also welcome if some part of the code are bad practise. Thanks!

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class CardLayoutTest extends JFrame implements ActionListener {

// Ref
 private JPanel cardPanel, jp1, jp2, buttonPanel;
 private JLabel jl1, jl2;
 private JButton btn1, btn2;
 private CardLayout cardLayout;

// Konstruktor
 public CardLayoutTest()
 {
     setTitle("Test med CardLayout");
     setSize(600,400);

     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");
     btn2 = new JButton("Show Card 2");

     buttonPanel.add(btn1);
     buttonPanel.add(btn2);

     getContentPane().add(cardPanel, BorderLayout.NORTH);
     getContentPane().add(buttonPanel, BorderLayout.SOUTH);

     btn1.addActionListener(this);
 }

     public void actionPerformed(ActionEvent event)
     {
        // ??? Show card 1 ???

        // ??? Show card 2 ???
     }

 public static void main(String[] args) {
     CardLayoutTest frame = new CardLayoutTest();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    } 
}

推荐答案

  • 我看不到 Java7 显示(容器父级,字符串名称)Java6 show(Container parent, String name) 已弃用

    取决于 currentCard 是否从 cl.show(cardPanel, "" + (currentCard));

    String>
  • depends if currentCard returns String from cl.show(cardPanel, "" + (currentCard));

    编辑(我试过你的代码示例)

    EDIT (I tried your code example)

    1.你忘了初始化最重要的变量

    1.you forgot to initialize most important variable

    private CardLayout cardLayout = new CardLayout();
    

    2.那么SSCCE可以是

    2.then SSCCE could be

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

    这篇关于如何在 CardLayout 中显示不同的卡片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆