【问题标题】:How can I switch between JPanels in a CardLayout using MVC?如何使用 MVC 在 CardLayout 中的 JPanel 之间切换?
【发布时间】:2014-12-20 22:19:01
【问题描述】:

我正在制作一个数学游戏应用程序,最近开始实现 MVC。

我有以下结构:

  • auiAs2

  • auiAs2.view

    • DiffView.java: 扩展 MigJPanel 实现 ScreenInterface

    • GameView.java: 扩展 MigJPanel 实现 ScreenInterface

    • EndGameView.java: 扩展 MigJPanel 实现 ScreenInterface

  • auiAs2.controller

  • auiAs2.model

我的MathsGame.java 包含一个设置为CardLayoutJPanel,其中添加了DiffViewGameViewEndGameView 的实例。当我运行我的程序时,diffView 'card' 会显示给用户。

如果用户点击“新游戏”,DiffControl.java 中的ActionListener 将获得所选难度。

public class DiffControl {
    private DiffView diffView;
    private Model model;

    public DiffControl(DiffView diffView, Model model) {
        this.diffView = diffView;
        this.model = model;

        this.diffView.addNewGameListener(new NewGameListener());
    }

    class NewGameListener implements ActionListener {
        String selectedDiff;
        @Override
        public void actionPerformed(ActionEvent e) {
            selectedDiff = diffView.getSelectedDiff();
            //MathsGame.setLayCard(panContainer, "New Game"));
        }
    }
}

这就是我卡住的地方。我应该在我的CardLayoutJPanellayCard 中的哪些面板之间切换? (MathsGame.java 如下所示,已删除无关代码。如果需要,相关类的整个代码在上面链接)

public class MathsGame extends JFrame {
    private JPanel panContainer = new JPanel();

    private CardLayout layCard = new CardLayout();

    public MathsGame() {
        panContainer.setLayout(layCard);
        setContentPane(panContainer);
        setSize(new Dimension(WIDTH, HEIGHT));
        setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
        setTitle(TITLE);

        DiffView panDiffView = new DiffView();
        panContainer.add(panDiffView, "Choose Difficulty");

        GameView panGameView = new GameView();
        panContainer.add(panGameView, "New Game");

        EndGameView panEndGameView = new EndGameView();
        panContainer.add(panEndGameView, "End Game");

        Model model = new Model();

        DiffControl diffControl = new DiffControl(panDiffView, model);
        //GameControl gameControl = new GameControl(panGameView, model);
        //EndGameControl EndGameControl = new EndGameControl(panEndGameView, model);

        layCard.show(panContainer, "Choose Difficulty");

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(MathsGame::new);
    }
}

所以我的问题是:

  • 在我的CardLayout 容器中放置与视图之间切换相关的代码的最佳位置是哪里?

【问题讨论】:

    标签: java swing design-patterns model-view-controller cardlayout


    【解决方案1】:

    模型将有一个状态字段,也许是一个枚举,它会反映视图。您可以使用 SwingPropertyChangeSupport 对象使其成为绑定属性。然后视图可以监听这个属性的状态并根据它的状态交换卡片。事实上,每个枚举常量的toString() 可用于将卡片视图添加到使用 CardLayout 的容器中。


    编辑
    你问:

    能否详细说明 SwingPropertyChangeSupport 的用法?

    您可以找到herehere,尤其是here 的示例。

    我尝试在模型中使用 addPropertyChangeListener。当 CardLayout 在 MathsGame.java 中时,视图将如何控制卡片的变化?

    视图会被通知模型的状态变化,然后当发生这种情况时,视图会调用它的代码来交换卡片。

    我在 View 中“从非静态上下文调用静态方法”时遇到了问题。

    这是一个完全不同的无关问题,一个基本的核心 Java 问题,我相信只要稍加努力,您就可以解决。简而言之——不要试图以静态方式调用实例代码。总是在适当的参考上调用它,而不是在课堂上。

    【讨论】:

    • 这很有帮助,谢谢!你能详细说明SwingPropertyChangeSupport的用法吗?我尝试在模型中使用addPropertyChangeListener。当CardLayoutMathsGame.java 中时,视图将如何控制卡片的变化?我在 View 内部“从非静态上下文调用静态方法”时遇到了问题。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多