简体   繁体   English

如何将堆栈窗格的不同场景设置为根布局JavaFX

[英]How to set diffrent scenes of stack pane into root layout JavaFX

I am a beginner programer and recently I have learned how to switch between scenes in JavaFX. 我是一名初学者,最近我学习了如何在JavaFX中的场景之间切换。 I would like to add a Border Pane (as a container for those scenes ) with some menu buttons, and set scenes in the center (and still be able to switch between them). 我想添加带有某些菜单按钮的“边框窗格”(作为这些场景的容器),并在中心设置场景(仍然可以在它们之间切换)。 What should I do or think of ? 我应该怎么做或想到? Here is my code: 这是我的代码:

   public class Main extends Application {

        public static final String ROOT = "Root";
        public static final String ROOT_FXML = "Root.fxml";
        public static final String FIRST_SCREEN = "FirstScreen";
        public static final String FIRST_SCREEN_FXML = "FirstScreen.fxml";
        public static final String SECOND_SCREEN = "SecondScreen";
        public static final String SECOND_SCREEN_FXML = "SecondScreen.fxml";



        @Override
        public void start(Stage primaryStage) {

            ScreensController mainContainer = new ScreensController();


            mainContainer.loadScreen(Main.FIRST_SCREEN, Main.FIRST_SCREEN_FXML);
            mainContainer.loadScreen(Main.SECOND_SCREEN, Main.SECOND_SCREEN_FXML);
            mainContainer.setScreen(Main.FIRST_SCREEN);

            Group root = new Group();
            root.getChildren().addAll(mainContainer);
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();

        }
    }

Interface class 接口类

  public interface ControlledScreen {

    public void setScreenParent(ScreensController screenPage);
}

Screens controller class 屏幕控制器类

public class ScreensController extends StackPane {

    private HashMap<String, Node> screens = new HashMap<>();

    public void addScreen(String name, Node screen) {
        screens.put(name, screen);
        System.out.println("added screen " + name);
    }

    public boolean loadScreen(String name, String resource) {
        try {
            FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
            Parent loadScreen = (Parent) myLoader.load();
            ControlledScreen myScreenControler = ((ControlledScreen) myLoader.getController());
            myScreenControler.setScreenParent(this);
            addScreen(name, loadScreen);
            return true;

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public boolean setScreen(final String name) {

        if (screens.get(name) != null) {
            if (!getChildren().isEmpty()) {
                getChildren().remove(0);
                // add new screen
                getChildren().add(0, screens.get(name));
            } else {
                getChildren().add(screens.get(name));
            }
            return true;
        } else {
            System.out.println("screen hasn't been loaded!\n");
            return false;
        }
    }

    public boolean unloadScreen(String name) {
        if (screens.remove(name) == null) {
            System.out.println("Screen didn't exist");
            return false;
        } else {
            return true;
        }
    }

}

At the moment I have two controller classes with two diffrend fxml files. 目前,我有两个带有两个diffrend fxml文件的控制器类。 They look like this: 他们看起来像这样:

public class FirstScreenController implements ControlledScreen {

    ScreensController myController;

    @FXML
    void initialize() {

    }

    @Override
    public void setScreenParent(ScreensController screenPage) {
        myController = screenPage;

    }
}

Is there a way to fix my code ? 有没有办法修复我的代码?

I solved problem. 我解决了问题。 Here is new main class 这是新的主班

    public class Main extends Application {

       public static final String ROOT = "Root";
    public static final String ROOT_FXML = "Root.fxml";
    public static final String FIRST_SCREEN = "FirstScreen";
    public static final String FIRST_SCREEN_FXML = "FirstScreen.fxml";
    public static final String SECOND_SCREEN = "SecondScreen";
    public static final String SECOND_SCREEN_FXML = "SecondScreen.fxml";

    private Stage primaryStage;
    private BorderPane rootLayout;

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        ScreensController mainContainer = new ScreensController();
        initRootLayout();

        mainContainer.loadScreen(Main.FIRST_SCREEN, Main.FIRST_SCREEN_FXML);
        mainContainer.loadScreen(Main.SECOND_SCREEN, Main.SECOND_SCREEN_FXML);
        mainContainer.setScreen(Main.FIRST_SCREEN);


        Group root = new Group();
        root.getChildren().addAll(mainContainer);
        rootLayout.setCenter(root);

    }

    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("view/Root.fxml"));
            rootLayout = (BorderPane) loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

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