【问题标题】:Javafx widget not responding after switching FXML切换 FXML 后 Javafx 小部件没有响应
【发布时间】:2021-06-11 04:14:59
【问题描述】:

在选择组合框中的项目后,我的 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);

谢谢。

【问题讨论】:

    标签: java fxml javafx-11


    【解决方案1】:

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

    • 将每种类型的计算器设为单独的Scene,然后通过调用方法setScene 切换Scenes。请注意,您可能需要将ComboBox 添加到每个Scene

    • 或者,将每个计算器设为单独的 Pane,并将其中的每一个 Panes 添加到 StackPane。通过使所选计算器的 Pane visible 和所有其他计算器不可见,在计算器 Panes 之间切换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多