具有不同 类 的卡片布局

Card Layout with different classes

我在为课程作业制作的小程序方面需要帮助。

我正在使用卡片布局,我希望每张卡片的内容取自不同的类。然后在每张卡片中我想要按钮切换到其他卡片。我见过以类似方式工作的示例,但它们基于 JFrame 而不是 applet。

请不要给我任何外部链接,我已经研究了好几天的卡片布局。我是 Java 初学者,而且学得慢。我希望有人给我看一个基本的例子并解释它是如何完成的。谢谢!

public class MainClass extends JApplet implements ActionListener {

        boolean inAnApplet = true;
        JButton btn1, btn2;
        JPanel cards;
        final static String PANEL1 = "";
        final static String PANEL2 = "";


        public MainClass() {

            Container contentPane = getContentPane();
            JPanel cbp = new JPanel();        
            cbp.add(new JLabel ("Hello world!"));

            contentPane.add(cbp, BorderLayout.NORTH);
            cards = new JPanel();
            cards.setLayout(new CardLayout());    


            JPanel p1 = new JPanel();
            p1.add(new JLabel("First panel!"));
            btn1 = new JButton("Text panel");
            btn1.addActionListener(this);
            p1.add(btn1);


            JPanel p2 = new JPanel();
            p2.add(new JLabel("Second panel!"));
            btn2 = new JButton("Button panel");
            btn2.addActionListener(this);
            p2.add(btn2);

            cards.add(p1, PANEL1);
            cards.add(p2, PANEL2);
            contentPane.add(cards, BorderLayout.CENTER);


        }

       public void init(){}

        public void actionPerformed(ActionEvent evt) {

            CardLayout cl = (CardLayout)(cards.getLayout());

                if(evt.getSource() == btn1)
                {
                    cl.show(cards, PANEL1);
                }
                else if(evt.getSource() == btn2)
                {
                    cl.show(cards, PANEL2);
                }   
           }
    }

您已将 JPanel 的布局管理器设置为 CardLayout。因此,卡片可以是可以添加到 JPanel 的任何 class - 即任何组件或组件的子 class。

The code that I've given is literally taken from that tutorial.

那么,为什么您的 PANEL1 和 PANEL2 变量没有值?如果您不指定卡的名称,您希望它如何工作???如果您不复制工作代码,您如何期望某些东西工作?当您甚至无法遵循教程中的代码时,我们为什么要提供工作代码?为什么您认为我们提供的任何代码都会有所不同?

but I don't understand how to make each card a different class.

没有区别。如果你想展示一张卡片,你需要知道你想要展示的卡片的名字。

现在您使用:

JPanel p1 = new JPanel();
cards.add(p1, PANEL1);

因此,如果面板位于不同的 class 中,则代码为:

JPanel p1 = new SomeCustomPanel()
cards.add(p1, PANEL1);

如果您想要将所有代码放在一个单独的面板中,那么您将需要从父面板获取布局管理器,因此每个面板的 ActionListener 中的代码可能类似于:

JPanel parent = childPanel.getParent();
CardLayout layout = (CardLayout)parent.getLayout();

现在您可以访问布局管理器,这样您就可以切换卡片,前提是您知道每张卡片的名称。如何命名每张卡片以及如何指定卡片名称由您决定。

编辑:

public class SomeCustomPanel extends JPanel
{
    public SomeCustomPanel
    {
        add( new JLabel("I'm a JLabel") );
        JButton button = new JButton("Switch Panel");
        add(button);
        button.addActionListener(...); // add ActionListener to swap cards
    }
}

编辑:

好吧,当您将组件添加到面板或将一个面板添加到另一个面板时,我使用这些名称来尝试显示组件之间的 parent/child 关系。

当您使用 ActionListener 时,ActionEvent 的来源将是 JButton。因此,根据生成 ActionEvent 的按钮,您可以返回 parent/child 链,直到找到使用 CardLayout 的面板。

所以你的代码会更像:

JButton button = (JButton)e.getSource();
JPanel someCustomPanel = (JPanel)button.getParent();
JPanel cardLayoutPanel = (JPanel)someCustomPanel.getParent();
CardLayout layout = (CardLayout)cardLayoutPanel.getLayout();
layout.show(cardLayoutPanel, "...");

现在您有了布局管理器,您只需要知道要显示的卡片的名称