如何在不使用静态的情况下从控制器(MVC)引用模型,反之亦然。 JavaFX 8

时间:2015-03-15 01:40:49

标签: 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);
        }
    }

编辑堆栈跟踪

   stack trace:     Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.java

1 个答案:

答案 0 :(得分:1)

这不是它的意思。

  

非静态变量CONVERTB无法从静态引用   上下文

这意味着这个 - > GameModel.CONVERTB

您尝试从静态(GameModel)上下文中引用它。变量不是静态的!

修复,除了实际使变量静态并执行您正在做的事情之外,将实例化GameModel并以这种方式访问​​它。知道这是一个 new GameModel,可能不是你需要的那个。这样做的方式是......

GameModel model = new GameModel(); 
model.CONVERTB ...;

请注意,这不是字段的正确命名约定,并给出了final或常量的印象。

如果您想要操作一个GameModel,很可能,您必须在控制器中拥有一个可以访问的游戏模型实例。

此外,我不会声明带有类别名称的大写字母的引用,因为你可以看到它会增加涉及静态的混乱。