对于JavaFX MVC应用程序,如何从控制器引用模型,以避免使用静态

时间:2015-02-20 13:09:58

标签: model-view-controller javafx

我确信这很简单,我误解了一些事情。我有一个链接到FXML(视图)的控制器类。控制器不包含任何静态,但是当我尝试像这样引用模型时:

 @FXML
 public void goToConvert(MouseEvent event){ 
     myController.setScreen(GameModel.CONVERT_SCREEN); 
     GameModel.CONVERTB = true;
     GameModel.PLAYB = false;
     GameModel.COMPAREB = false;
     GameModel.SETTINGSB = false;
     GameModel.HELPB = false;
     GameModel.ProcessSreeenSwitch();

GameModel是我的模特
CONVERTB是一个公共布尔值 ProcessScreeenSwitch()是公共空白

不采取任何措施在控制器中包含对GameModel的对象引用,使用GameModel的每一行都会产生常见错误:

non-static variable CONVERTB cannot be referenced from a static context

即使控制器中没有任何静力学。我不明白为什么当前的背景是静态的。我已经尝试将GameModel声明为上面的对象(例如,在ScreensController myController的底部代码中声明了相同的位置)

 GameModel GameModel = new GameModel();

 GameModel GameModel;

这些度量会停止静态错误,但在调用它们时会创建NullPointerException。例如,如果调用goToConvert,则NullPointerException位于以下行:

GameModel.CONVERTB = true;

如何使当前上下文非静态从控制器更改GameModel中的值。

干杯

卢西奥

Controller Class:
    package aimdot;

import java.awt.MouseInfo;
import java.awt.Point;
import java.io.File;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class PlayScreenController implements Initializable, 
                                            ControlledScreen { 

     ScreensController myController; 
     //final static Image MOUSECURSOR = new Image (PlayScreenController.class.getResource("MouseCursor").toString());
     //final static Image MOUSECURSOR_IMAGE = new Image (PlayScreenController.class.getResource("MouseCursor").toString());
    // final static Image MOUSECURSOR_IMAGE = new Image (PlayScreenController.class.getResource("MouseCursor").toString());
    @FXML
    private ImageView imageView;
     @FXML
    private ImageView MouseCursor;
      @FXML
    private ImageView RectangleMouseHolder;
       @FXML
    private ImageView Target3Rings;
        @FXML
    private Label PLAYBUTTON;


     @Override
     public void initialize(URL url, ResourceBundle rb) {


            } 


     public void setScreenParent(ScreensController screenParent){ 
        myController = screenParent; 
     }   

     @FXML 
     private void PLAYBUTTONPressed(MouseEvent event){ 
     PLAYBUTTON.setTranslateX(-400);
     GameModel.AnimationTargetStart();
     PLAYPHASE();
   } 
    @FXML
    public void TargetMissed(ActionEvent event){ 


   }


     @FXML
    public void goToPlay(MouseEvent event){ 
     myController.setScreen(GameModel.PLAY_SCREEN); 
     GameModel.CONVERTB = false;
     GameModel.PLAYB = true;
     GameModel.COMPAREB = false;
     GameModel.SETTINGSB = false;
     GameModel.HELPB = false;
     GameModel.ProcessSreeenSwitch();
   } 
     @FXML 
     private void goToMain(ActionEvent event){ 
     myController.setScreen(GameModel.MAIN_MENU); 
     GameModel.CONVERTB = false;
     GameModel.PLAYB = false;
     GameModel.COMPAREB = false;
     GameModel.SETTINGSB = false;
     GameModel.HELPB = false;
     GameModel.ProcessSreeenSwitch();
   } 


    @FXML
    public void goToConvert(MouseEvent event){ 
     myController.setScreen(GameModel.CONVERT_SCREEN); 
     GameModel.CONVERTB = true;
     GameModel.PLAYB = false;
     GameModel.COMPAREB = false;
     GameModel.SETTINGSB = false;
     GameModel.HELPB = false;
     GameModel.ProcessSreeenSwitch();
   } 

    @FXML
    public void goToCompare(MouseEvent event){ 
     myController.setScreen(GameModel.COMPARE_SCREEN); 
     GameModel.CONVERTB = false;
     GameModel.PLAYB = false;
     GameModel.COMPAREB = true;
     GameModel.SETTINGSB = false;
     GameModel.HELPB = false;
     GameModel.ProcessSreeenSwitch();
   } 

   @FXML
    public void goToSettings(MouseEvent event){ 
     myController.setScreen(GameModel.SETTINGS_SCREEN);
     GameModel.CONVERTB = false;
     GameModel.PLAYB = false;
     GameModel.COMPAREB = false;
     GameModel.SETTINGSB = true;
     GameModel.HELPB = false;
     GameModel.ProcessSreeenSwitch();
   }        

   @FXML
    public void goToHelp(MouseEvent event){ 
     myController.setScreen(GameModel.HELP_SCREEN); 
     GameModel.CONVERTB = false;
     GameModel.PLAYB = false;
     GameModel.COMPAREB = false;
     GameModel.SETTINGSB = false;
     GameModel.HELPB = true;
     GameModel.ProcessSreeenSwitch();
   }  

    @FXML
    public void MouseLocation(MouseEvent event){ 

   }
    @FXML
    public void RestartButton(MouseEvent event){ 

       GameModel.RestartRound();

   }




 public void PLAYPHASE(){
     boolean targetSpawned = false;

     GameModel.playPhase = true;


     long gameStartMS = System.currentTimeMillis();
     long gameStartS = TimeUnit.MILLISECONDS.toSeconds(gameStartMS);


     }


 }

已尝试添加步骤的编辑。

尝试使用

 GameModel Model = new GameModel();

在每个控制器中,并且从控制器中消除了GameModel的使用,但它没有起作用。它仍然在

处创建一个空指针异常
Model.ProcessSreeenSwitch(); 

在GameModel类中:

public void ProcessSreeenSwitch(){
        System.out.print("PlayB: "+PLAYB);
        if (PLAYB == false){
            target.setVisible(false);
            PlayLabel.setVisible(false);
            PutMouseMiddle.setVisible(false);
            hitCountG.setVisible(false);
            missCountG.setVisible(false);
            rectangleMouseBoxG.setVisible(false);
           RestartPlayButton.setVisible(false);
           PlayTimer.setVisible(false);
           PlayTimerText.setVisible(false);
        }
        if (PLAYB == true){
            target.setVisible(true);
            PlayLabel.setVisible(true);
            PutMouseMiddle.setVisible(true);
            hitCountG.setVisible(true);
            missCountG.setVisible(true);
           rectangleMouseBoxG.setVisible(true);
           RestartPlayButton.setVisible(true);
           PlayTimer.setVisible(true);
           PlayTimerText.setVisible(true);
        }
    } 

0 个答案:

没有答案