如何使用MVC在CardLayout中的JPanels之间进行切换? [英] How can I switch between JPanels in a CardLayout using MVC?

查看:205
本文介绍了如何使用MVC在CardLayout中的JPanels之间进行切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我有以下结构:




  • auiAs2




    • 一个href =http://pastebin.com/njXKcqdp =nofollow noreferrer> MigJPanel : extends JPanel


    • 此处,特别是这里


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


      该视图将被通知模型的状态更改,然后在发生这种情况时,视图将调用其代码进行交换


      我在View中从非静态上下文调用静态方法中遇到问题。 p>

      这是一个完全不同的无关问题,一个基本的核心Java问题,我相信有一点工作,你会解决简单来说 - 不要以静态方式调用实例代码。总是把它称为适当的参考,而不是上课。


      I'm making a Maths Game application and recently began implementing MVC.

      I have the following structure:

      • auiAs2

      • auiAs2.view

        • DiffView.java: extends MigJPanel implements ScreenInterface

        • GameView.java: extends MigJPanel implements ScreenInterface

        • EndGameView.java: extends MigJPanel implements ScreenInterface

      • auiAs2.controller

      • auiAs2.model

      My MathsGame.java contains a JPanel set to CardLayout which instances of DiffView, GameView, and EndGameView are added to. When I run my program, the diffView 'card' is shown to the user.

      If the user clicks "New Game", an ActionListener in DiffControl.java gets the selected difficulty.

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

      This is where I get stuck. Where should I switch between panels in my CardLayout JPanel layCard? (MathsGame.java is shown below with irrelevant code removed. The entire code for relevant classes is linked above if required)

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

      So my question is:

      • Where would be the best place to put code related to switching between Views in my CardLayout container?

      解决方案

      The model will have a state field, perhaps an enum, that would reflect the view. You could make this a bound property using a SwingPropertyChangeSupport object. Then the view could listen to the state of this property and swap the Card based on its state. In fact the toString() of each enum constant could be used to add card views to the CardLayout-using container.


      Edit
      You ask:

      Could you elaborate on the usage of SwingPropertyChangeSupport?

      You can find examples of this here, here, and especially here.

      I've attempted using addPropertyChangeListener in the model. And how would the View control the card change when the CardLayout is in MathsGame.java?

      The view would be notified of the state change of the model, and then when this happens, the view would call its code to swap cards.

      I've had problems with doing that inside View for "calling a static method from a non-static context".

      This is a completely different unrelated issue, a basic core Java issue, that I'm sure with a little work, you'll be able to solve. In brief -- don't try to call instance code in a static way. Always call it on a proper reference, not on the class.

      这篇关于如何使用MVC在CardLayout中的JPanels之间进行切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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