首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX,在同一阶段的不同场景中拥有相同的对象/项目?

JavaFX,在同一阶段的不同场景中拥有相同的对象/项目?
EN

Stack Overflow用户
提问于 2018-04-17 17:28:07
回答 1查看 1.1K关注 0票数 1

我正在使用JavaFX开发一个应用程序,其中我需要在多个场景之间进行切换。但似乎我不能在多个场景中拥有相同的项目(例如:工具栏),它只能在其中一个场景中显示该项目。也许不可能在不同的场景中拥有相同的物品,所以我的问题是,我该如何做到这一点?我需要多个阶段吗?如果是这样,我如何在阶段之间进行更改?我没有在这个项目中使用FXML,我们必须对它进行编码。我当前的代码:

代码语言:javascript
复制
public class Main extends Application {

    private Label time;

    private int minute;
    private int hour;
    private int second;


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

    // CLOCK RUNNING

    public void initialize() {

            Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
                Calendar cal = Calendar.getInstance();
                second = cal.get(Calendar.SECOND);
                minute = cal.get(Calendar.MINUTE);
                hour = cal.get(Calendar.HOUR);
                //System.out.println(hour + ":" + (minute) + ":" + second);
                time.setText(hour + ":" + (minute) + ":" + second);
            }),
                    new KeyFrame(Duration.seconds(1))
            );
            clock.setCycleCount(Animation.INDEFINITE);
            clock.play();
        }


    @Override
    public void start(Stage primaryStage) throws Exception {

        //Specify The Size of Scenes, and the scenes.
        BorderPane root1 = new BorderPane();
        BorderPane root2 = new BorderPane();
        Scene scene1 = new Scene(root1, 1100, 900);
        Scene scene2 = new Scene(root2,1100,900);


        // Get CSS File
        scene1.getStylesheets().add("Helmuth.css");


        time = new Label("Time:");
        initialize();


        //ToolBar i want this to be shown in both scenes //

        Button homebt = new Button("Home");
        Button tabelbt = new Button("Tabel");


        ToolBar toolBar = new ToolBar();
        toolBar.getItems().add(homebt);
        toolBar.getItems().add(tabelbt);
        toolBar.getItems().add(time);


        Label label1 = new Label("Welcome to the first scene!");
        Button button1 = new Button("Go to scene 2");
        button1.setOnAction(e -> primaryStage.setScene(scene2));

        VBox layout1 = new VBox();
        layout1.getChildren().addAll(button1,toolBar);

        Button button2 = new Button("Go Back");
        button2.setOnAction(e -> primaryStage.setScene(scene1));

        VBox mainbox = new VBox();
        mainbox.setAlignment(Pos.TOP_CENTER);
        mainbox.getChildren().addAll(button2, toolBar);


        // Start scene 1
        root2.setCenter(mainbox);
        root1.setCenter(layout1);
        primaryStage.setScene(scene1);
        primaryStage.setTitle("Helmuth");
        boolean b = false;
        primaryStage.setResizable(b);
        primaryStage.show();
    }
}                                                                                           
EN

回答 1

Stack Overflow用户

发布于 2018-04-17 17:37:46

你为什么要在不同的场景之间切换。问题的解决方案可能是仅交换场景的根节点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49874484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档