切换 FXML 后 Javafx 小部件没有响应

Javafx widget not responding after switching FXML

我的 javafx 应用程序必须在选择组合框中的项目后切换其 FXML 文件然而,组合框在切换后停止响应,但 FXML 添加的其他项目继续工作。我必须最小化和最大化应用程序以使其工作或使用 FXML 添加的小部件以使其响应。

comboBox.setOnAction(event -> {
        calculatorType = comboBox.getValue();
        switch (calculatorType) {
            case "Basic":
                System.out.println(root.getChildren());
                root.getChildren().clear();
                try {
                    stage.setMinWidth(430);
                    stage.setMinHeight(530);
                    stage.setMaxWidth(430);
                    stage.setMinHeight(530);
                    //root.getChildren().remove(comboBox);
                    root.getChildren().add(FXMLLoader.load(getClass().getResource("/Calculator/Resources" +
                            "/BasicCalculator.fxml")));
                    comboBox.setLayoutX(320);
                    comboBox.setLayoutY(0);
                    root.getChildren().add(comboBox);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case "Scientific":
                root.getChildren().clear();
                try {
                    stage.setMinHeight(530);
                    stage.setMinWidth(780);
                    stage.setMaxHeight(530);
                    stage.setMaxWidth(780);
                    root.getChildren().add(FXMLLoader.load(getClass().getResource("/Calculator/Resources/" +
                            "ScientificCalculator.fxml")));
                    comboBox.setLayoutX(675);
                    comboBox.setLayoutY(0);
                    root.getChildren().add(comboBox);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case "Converter":
                root.getChildren().clear();
                stage.setMinHeight(530);
                stage.setMinWidth(430);
                stage.setMaxHeight(530);
                stage.setMaxWidth(430);
                try {
                    root.getChildren().add(FXMLLoader.load(getClass().getResource("/Calculator/Resources/" +
                                "Converter.fxml")));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                comboBox.setLayoutX(320);
                comboBox.setLayoutY(0);
                root.getChildren().add(ComboPane);
                break;
        }
});

组合框在main.

组合框代码:

ComboBox<String> comboBox = new ComboBox<>();
        comboBox.getStylesheets().add("Calculator/Resources/Styles/ComboBox.css");
        comboBox.getItems().add("Basic");
        comboBox.getItems().add("Scientific");
        comboBox.getItems().add("Converter");
        comboBox.getSelectionModel().select(0);
        comboBox.setLayoutX(320);
        comboBox.setLayoutY(0);

谢谢。

我建议您使用不同的设计。以下是两种可能性。

  • 将每种类型的计算器单独制作Scene and just switch Scenes by calling method setScene。请注意,您可能需要将 ComboBox 添加到每个 Scene.

  • 或者,使每个计算器成为一个单独的 Pane and add each of those Panes to a StackPane. Switch between the calculator Panes by making the selected calculator's Pane visible,而其他所有计算器都不可见。