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

查看:102
本文介绍了在一个类中使用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);
}

ButtonPanel

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天全站免登陆