Javafx переключение сцен с использованием разных панелей - PullRequest
1 голос
/ 02 мая 2020

У меня есть класс SceneLibrary, который я хотел бы использовать для переключения сцен. При запуске приложения у меня возникают ошибки в PaneOne private SceneLibrary sceneLibrary = new SceneLibrary(); и ошибки в SceneLirary private Scene sceneOne = new Scene(new PaneOne(),300,500);. Я не могу использовать stati c сцены.

public class SceneLibrary {
    private Stage primaryStage;
    private Scene sceneOne = new Scene(new PaneOne(),300,500);
    private Scene sceneTwo = new Scene(new PaneTwo(),400,500);

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

    public void switchToOne(){
        primaryStage.setScene(sceneOne);
    }

    public void switchToTwo(){
        primaryStage.setScene(sceneTwo);
    }
}

Мой класс PaneOne:

public class PaneOne extends AnchorPane {
    private SceneLibrary sceneLibrary = new SceneLibrary();
    public PaneOne() {
        Button button = new Button();
        this.getChildren().add(button);

        button.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                sceneLibrary.switchToTwo();
            }
        });
    }
}

Мой Основной класс:

public class Main extends Application {
    private SceneLibrary sceneLibrary = new SceneLibrary();
    @Override
    public void start(Stage primaryStage) throws Exception{
        AnchorPane root = new PaneOne();
        primaryStage.setTitle("Hello World");
        sceneLibrary.setPrimaryStage(primaryStage);
        primaryStage.setScene(new Scene(root, 300, 500));
        primaryStage.show();
    }

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

1 Ответ

1 голос
/ 02 мая 2020

Поделиться экземпляром sceneLibrary:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        SceneLibrary sceneLibrary = new SceneLibrary();
        sceneLibrary.setPrimaryStage(primaryStage);
        sceneLibrary.switchToOne();//set scene one to the stage 
        primaryStage.show();
    }

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

class PaneOne extends AnchorPane {

    public PaneOne(SceneLibrary sceneLibrary) {
        Button button = new Button("Switch To Scene 2");
        this.getChildren().add(button);
        button.setOnAction(event -> sceneLibrary.switchToTwo());
    }
}

class PaneTwo extends AnchorPane {

    public PaneTwo(SceneLibrary sceneLibrary) {
        Button button = new Button("Switch To Scene 1");
        this.getChildren().add(button);
        button.setOnAction(event -> sceneLibrary.switchToOne());
    }
}

class SceneLibrary {

    private Stage primaryStage;
    private final Scene sceneOne = new Scene(new PaneOne(this),300,500);
    private final Scene sceneTwo = new Scene(new PaneTwo(this),400,500);

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

    public void switchToOne(){
        primaryStage.setScene(sceneOne);
    }

    public void switchToTwo(){
        primaryStage.setScene(sceneTwo);
    }
}
...