简体   繁体   English

JavaFX通过阶段/切换场景

[英]JavaFX Passing Stage/Switching Scene

I have decided to update my application using JavaFXML, However I am having difficulties passing a scene into my controller. 我已经决定使用JavaFXML更新我的应用程序,但是我很难将场景传递到控制器中。 Here is my Controller; 这是我的控制器;

public class MainApp extends Application {

@FXML
public Stage primaryStage;

@FXML
private AnchorPane rootLayout;
@FXML
private JobInterface jInterface;

@Override
public void start(Stage primaryStage) {
    primaryStage = new Stage();
    setPrimaryStage(primaryStage);
    initRootLayout();
}

@FXML
public void initRootLayout(){
    try {
        primaryStage = getPrimaryStage();
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("MainInterface.fxml"));
        rootLayout = (AnchorPane) loader.load();        
        Scene scene = new Scene(rootLayout);    
        primaryStage.setScene(scene);
        primaryStage.show();
        setPrimaryStage(primaryStage);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@FXML
private void setJobLayout(){
    primaryStage = getPrimaryStage();
    jInterface = new JobInterface();
    jInterface.initJobLayout();
    primaryStage.setScene(jInterface.getScene());
}

public static void main(String[] args) {
    launch(args);
}

public Stage getPrimaryStage() {
    return primaryStage;
}

public void setPrimaryStage(Stage primaryStage) {
    this.primaryStage = primaryStage;
}
}

Here is a method that is changing the scene using a different FXML file and attempting to pass the scene back to the controller; 这是一种使用不同的FXML文件更改场景,然后尝试将场景传递回控制器的方法。

public class JobInterface {

private AnchorPane rootLayout;
private Scene scene;

public void initJobLayout(){
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("JobInterface.fxml"));
        rootLayout = (AnchorPane) loader.load();
        scene = new Scene(rootLayout);
        setScene(scene);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public Scene getScene() {
    return scene;
}

public void setScene(Scene scene) {
    this.scene = scene;
}
}

The issue I'm having now is a NullPointerException on this line in the main app; 我现在遇到的问题是主应用程序中此行上的NullPointerException。

primaryStage.setScene(jInterface.getScene());

I am trying to pass a Stage between methods so that I can only update the Scene and not have to open a new Stage everytime a new method is called. 我试图在方法之间传递一个Stage,以便我只能更新Scene,而不必在每次调用新方法时都打开一个新Stage。 Any ideas on where I am going wrong? 关于我要去哪里的任何想法?

There should be no need to pass the stage or scene. 无需通过舞台或场景。 Your Main will load the fxml resource which will have your fxml controller 'in charge' of your fxml file. Main将加载fxml资源,该资源将由fxml控制器“负责” fxml文件。

    @Override
    public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("jobController.fxml"));        
    Scene scene = new Scene(root);        
    stage.setScene(scene);
    stage.show();
}

Your controller might look something like this (depending on your fxml design): 您的控制器可能看起来像这样(取决于您的fxml设计):

public class JobController {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {        
        label.setText("This is the controller speaking");
    } 
}

Now you can 'control' your stage (scene) from the controller. 现在,您可以从控制器“控制”您的舞台(场景)。 If you are going to create another class which also has to update the scene, pass a reference to the controller to it eg from the controller: 如果要创建另一个也必须更新场景的类,则将对控制器的引用传递给它,例如从控制器:

TimeClock timeClock = new TimeClock();
timeClock.init(this);

and then in TimeClock.java you have: 然后在TimeClock.java中,您可以:

private final JobController controller;

public void init (JobController control){ 
    this.controller = control;
}

Now you can access any public method in your controller from the TimeClock class. 现在,您可以从TimeClock类访问控制器中的任何公共方法。 Eg 例如

controller.updateLabel("Time clock speaking!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM