在一个类中使用 JButton 来更改另一个类中的卡片(使用 CardLayout) [英] Using JButton in one class to change card in another class (using CardLayout)

查看:38
本文介绍了在一个类中使用 JButton 来更改另一个类中的卡片(使用 CardLayout)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个 GUI,其中使用不同的扩展面板类将多个 JPanel 组件相互嵌套.在我的 Menue 中,我有一个 CenterPanel 和一个 ButtonPanel.ButtonPanel 包含实现了 ActionListener 接口的按钮.CenterPanel 使用 CardLayout,我想使用上述按钮来更改 CenterPanel 中的卡片.问题是,按钮对 CenterPanel 或该面板中的卡片一无所知.

I build a GUI in which I nested several JPanel components into each other using different classes extending panel. In my Menue I have a CenterPanel and a ButtonPanel. The ButtonPanel contains buttons which implemented ActionListener interfaces. The CenterPanel uses CardLayout and I want to use said buttons to change the cards in the CenterPanel. The problem is, the buttons don't know anything about the CenterPanel or the cards in that panel.

我想避免将所有内容都放在一个班级中,但我不知道如何在不只在一个班级中编写所有内容的情况下解决这个问题?

I wanted to avoid putting all in just one class for the menue, but I don't know How to solve this without just writing everyting in one class?

这是我的一些代码:

public class MenuePanel extends JPanel{

JButton startButton;
JButton exitButton;
JButton anleitungButton;
ButtonPanel buttonPanel;
TitlePanel titlePanel;
JPanel centerPanel;
CardLayout cardLayout;
TextPanel anleitungPanel;
TextPanel gameOverPanel;
TextPanel gameWonPanel;
TextPanel nextLevelPanel;
StartPanel startPanel;

public MenuePanel() {
    super();
    cardLayout = new CardLayout();
    setBackground(Color.CYAN);
    setBounds(0,0,800,600);
    setLayout(new BorderLayout());

    titlePanel = new TitlePanel();
    add(titlePanel, BorderLayout.PAGE_START);

    startPanel = new StartPanel();
    gameOverPanel = new TextPanel("Game Over!");
    gameWonPanel = new TextPanel("Game Won!");
    anleitungPanel = new TextPanel();
    nextLevelPanel = new TextPanel("Next Level!");
    centerPanel = new JPanel();

    centerPanel.setLayout(cardLayout);
    add(centerPanel, BorderLayout.CENTER);
    centerPanel.add(startPanel, "1");
    centerPanel.add(gameOverPanel, "2");
    centerPanel.add(gameWonPanel, "3");
    centerPanel.add(anleitungPanel, "4");
    centerPanel.add(nextLevelPanel, "5");
    cardLayout.show(centerPanel, "2");

    buttonPanel = new ButtonPanel();
    add(buttonPanel, BorderLayout.PAGE_END);
    setVisible(true);
}

按钮面板

public class ButtonPanel extends JPanel{

JButton startButton;
JButton exitButton;
JButton anleitungButton;

public ButtonPanel() {
    super();
    setLayout(new FlowLayout());
    setPreferredSize(new Dimension(800, 100));
    setBackground(Color.CYAN);

    startButton = new JButton("start)");
    startButton.setPreferredSize(new Dimension(150,50));
    add(startButton);
    anleitungButton = new JButton("anleitung");
    anleitungButton.setPreferredSize(new Dimension(150,50));
    add(anleitungButton);
    exitButton = new JButton("exit");
    exitButton.setPreferredSize(new Dimension(150,50));
    add(exitButton);

    CloseListener closeListener = new CloseListener();
    StartListener startListener = new StartListener();
    AnleitungListener anleitungListener = new AnleitungListener();
    startButton.addActionListener(startListener);
    anleitungButton.addActionListener(anleitungListener);
    exitButton.addActionListener(closeListener);

}

TextPanel

public class TextPanel extends JPanel{
JLabel text;

public TextPanel(String panel) {
    super();
    text = new JLabel(panel, SwingConstants.CENTER);
    setPreferredSize(new Dimension(800, 400));
    text.setFont(new Font("Arial", Font.ITALIC, 75));
    add(text);
}

AnleitungListener

public class AnleitungListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {

}

我是否做错了这一切,需要改变整个架构?如果,如何?大多数关于如何使用 ActionListeners 的例子要么只是写在一个类中,要么是复杂的让我理解.

Am I doing this whole thing wrong and need to change the whole architecture? And if, how? Most examples on how to use ActionListeners are either just written in one class or way to complex for me to understand.

推荐答案

您可以在两者之间创建一个类来协调整个切换,以防止在类之间共享大量实例.我已经使用 CardSwitcher 创建了一个示例,它正是这样做的.

You can create a class in between to orchestrate the whole switching, to prevent sharing lots of instances between classes. I have created an example using a CardSwitcher that does exactly that.

如果您觉得它有用,请告诉我.

Let me know if you found it useful.

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

public class Test extends JPanel {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Test");

        Test tst = new Test();
        frame.getContentPane().add(tst);

        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    public Test() {
        setLayout(new BorderLayout());
        CardLayout cardLayout = new CardLayout();

        JPanel centerPanel = new JPanel(cardLayout);

        JPanel panelA = new JPanel();
        panelA.add(new JLabel("Panel A"));
        centerPanel.add(panelA, "A");
        JPanel panelB = new JPanel();
        panelB.add(new JLabel("Panel B"));
        centerPanel.add(panelB, "B");
        JPanel panelC = new JPanel();
        panelC.add(new JLabel("Panel C"));
        centerPanel.add(panelC, "C");

        CardSwitcher switcher = new CardSwitcher(centerPanel, cardLayout);
        ButtonPanel buttons = new ButtonPanel(switcher);

        add(centerPanel, BorderLayout.CENTER);
        add(buttons, BorderLayout.SOUTH);


    }

    class ButtonPanel extends JPanel {
        public ButtonPanel(CardSwitcher switcher) {
            setLayout(new FlowLayout());

            JButton buttonA = new JButton("Show A");
            buttonA.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    switcher.switchTo("A");
                }
            });
            add(buttonA);

            JButton buttonB = new JButton("Show B");
            buttonB.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    switcher.switchTo("B");
                }
            });
            add(buttonB);

            JButton buttonC = new JButton("Show C");
            buttonC.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    switcher.switchTo("C");
                }
            });
            add(buttonC);

        }
    }


    class CardSwitcher {
        CardLayout layout;
        Container container;

        public CardSwitcher(Container container, CardLayout layout) {
            this.layout = layout;
            this.container = container;
        }

        public void switchTo(String card) {
            layout.show(container, card);
        }
    }
}

这篇关于在一个类中使用 JButton 来更改另一个类中的卡片(使用 CardLayout)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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