在一个类中使用JButton来更改另一个类中的卡(使用CardLayout)

时间:2017-11-29 13:47:38

标签: java swing architecture actionlistener

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

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

以下是我的一些代码:

MenuePanel

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) {

}

我做错了这件事,需要改变整个架构吗?如果,如何?关于如何使用ActionListener的大多数示例要么只是在一个类中编写,要么是复杂的,以便我理解。

1 个答案:

答案 0 :(得分:0)

您可以在两者之间创建一个类来编排整个切换,以防止在类之间共享大量实例。我创建了一个使用CardSwitcher的例子。

如果您发现它有用,请告诉我。

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