Changing Scenes in JavaFX NullPointerException

僤鯓⒐⒋嵵緔 提交于 2019-11-28 06:05:46

问题


I'm wanting to change to a scene in another class but I'm having great difficulty.

Now the I can move to the to the second screen no problem, but moving back to the first screen gives me the NullPointerException.

Help would be much appreciated. Many thanks in advance.

Main Class

public class Main extends Application {

Stage ps;
Group root = new Group();
Scene s = new Scene(root, 300, 300, Color.AQUA);
Controller con = new Controller();


public void start(Stage primaryStage) throws Exception {
    ps = primaryStage;
    con.buttonLayout();
    buttonLayout();

    primaryStage.setTitle("Hello World");
    ps.setScene(s);
    primaryStage.show();
}

public void buttonLayout() {
    Button but = new Button("first");

    but.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            ps.setScene(con.s);
        }
    });

    root.getChildren().add(but);
}


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

Other class

public class Controller{
Group root = new Group();
Scene s = new Scene(root, 300, 300, Color.BLACK);


public void buttonLayout() {
    Button but = new Button("back to first");

    but.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            Main main = new Main();
            main.ps.setScene(main.s);
        }
    });

    root.getChildren().add(but);
}
}

回答1:


Restructure your application, create two controllers instead of just one. So you have one controller for each scene. And in your main application, just invoke the first controller to set your first scene.

Here is a sample:

  • Small JavaFX framework for swapping in and out child panes in a main FXML container.
  • Loading new fxml in the same scene

An additional simplified example which does not cache scenes or controllers, does not use FXML and completely replaces the content of the scene on each navigation:

Main.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
    public void start(Stage stage) throws Exception {
        Controller1 controller1 = new Controller1();
        Scene scene = new Scene(
                controller1.getContent(), 300, 300, Color.AQUA
        );

        stage.setScene(scene);
        stage.show();
    }

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

Controller.java

import javafx.scene.Parent;

public interface Controller {
    Parent getContent();
}

Controller1.java

import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;

public class Controller1 implements Controller {
    private final StackPane root;

    public Controller1() {
        Button navToSecond = new Button("second");

        navToSecond.setOnMouseClicked(event -> {
            Controller2 controller2 = new Controller2();
            navToSecond.getScene().setRoot(controller2.getContent());
        });

        root = new StackPane(navToSecond);
    }

    @Override
    public Parent getContent() {
        return root;
    }
}

Controller2.java

import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;

public class Controller2 implements Controller {
    private final StackPane root;

    public Controller2() {
        Button navToFirst = new Button("first");

        navToFirst.setOnMouseClicked(event -> {
            Controller1 controller1 = new Controller1();
            navToFirst.getScene().setRoot(controller1.getContent());
        });

        root = new StackPane(navToFirst);
    }

    @Override
    public Parent getContent() {
        return root;
    }
}


来源:https://stackoverflow.com/questions/36463725/changing-scenes-in-javafx-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!