【问题标题】:JavaFX, Having the same object/item in different scenes in the same stage?JavaFX,在同一阶段的不同场景中拥有相同的对象/项目?
【发布时间】:2018-09-27 04:57:56
【问题描述】:

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

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();
    }
}                                                                                           

【问题讨论】:

  • Nodes 仅限于最多 1 个父级和最多 1 个场景。尝试在多个地方使用它要么将其从旧父级中删除,要么导致异常。您可以替换场景的一部分,例如使用BorderPane.setCenter 或通过修改某些父级的子列表,因此重用场景的某些部分不会被排除在外。但是,如果您需要同时在多个位置/场景中显示一个节点,您唯一的选择是创建一个副本。在这种情况下,使用方法创建部分场景可以减少代码重复。
  • 好的,谢谢!所以要么是重复代码,要么是制作可以在多个场景中调用的方法?
  • 使用这个库。它提供了一个具有附加功能的 javafx Stage 对象:github.com/Oshan96/CustomStage(要更改场景,您只需执行“customStage.changeScene(yourNode);”所有内容都在其自述文件和 wiki 中进行了解释:github.com/Oshan96/CustomStage/wiki

标签: java user-interface javafx scene stage


【解决方案1】:

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

【讨论】:

  • 这是什么意思,我对 java 和 javaFX 还很陌生?
  • 每个场景都有一个根节点,并且可以更改。因此,您无需更改场景,只需设置一个新的根节点。
猜你喜欢
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 2017-04-13
  • 2015-01-08
  • 1970-01-01
相关资源
最近更新 更多