当前位置: 首页 > 知识库问答 >
问题:

JAVA和JAVAFX问题-尝试将附加控制器连接到主控制器

易祖鹤
2023-03-14

大家下午好。我通常会自己发现并修正错误,但这次我真的卡住了。我的作业是写一个贷款计算器。所有代码都正常工作,编译也很好,直到我需要创建一个线图/图形,它弹出在一个新窗口中。
问题出在加载FXML文件或将其他控制器连接到主控制器的某个地方。
我尝试了不同的方法,并在不同的论坛上查看了解决方案,但无法在代码中实现。有人能给我提个解决办法吗?

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Scene.fxml"));
        primaryStage.setTitle("Loan calculator");
        primaryStage.setScene(new Scene(root, 770, 410));
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
public class Controller implements Initializable {
    public static int years = 0;
    public static int months = 0;
    private double desiredLoan = 1; //should be set to zero,but for testing is set differently

    private boolean graph = true; //true - linear, false - annuity

    @FXML
    private Button Button_3 = new Button();

    private LineGraphController lineGraphController = new LineGraphController("Linear");
    private AnnuityGraphController annuityGraphController = new AnnuityGraphController("Annuity");


/**Some code to count my data*/

    @Override /** This method is used to access my UI elements and access other controllers*/
    public void initialize(URL url, ResourceBundle resourceBundle) {
        Button_3.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                try {
                    if (desiredLoan == 0 && months == 0 && years == 0) {
                        throw new RuntimeException();
                    }
                    else {
                        if (whatGraph() == true) { //make linear graph
                            lineGraphController.initialize(url, resourceBundle);
                        }
                        else {//make annuity graph
                            annuityGraphController.initialize(url, resourceBundle);
                        }
                    }
                }
                catch (RuntimeException error) {
                    error.printStackTrace();
                }
            }
        });
    }

    /** Getters and setters */
    public boolean whatGraph() {
        return graph;
    }
    public void setGraph(boolean graph) {
        this.graph = graph;
    }
}

/** This controller is used to load additional fxml file*/
public class LineGraphController implements Initializable {
    @FXML
    public LineChart<?, ?> LineGraph;
    private String title;

    public LineGraphController(String title) {
        this.title = title;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("LineGraph.fxml"));
        Parent lineGraph = null;
        try {
            lineGraph = (Parent)fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle(title);
        window.setResizable(false);
        window.setMinWidth(600);
        window.setMinHeight(400);
        window.setScene(new Scene(lineGraph));
        window.showAndWait();
    }
}
/** This controller is used to load additional fxml file*/
public class AnnuityGraphController implements Initializable {
    @FXML
    public LineChart<?, ?> AnnuityGraph;
    private String title;

    public AnnuityGraphController(String title) {
        this.title = title;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("AnnuityGraph.fxml"));
        Parent lineGraph = null;
        try {
            lineGraph = (Parent)fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle(title);
        window.setResizable(false);
        window.setMinWidth(600);
        window.setMinHeight(400);
        window.setScene(new Scene(lineGraph));
        window.showAndWait();
    }
}
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #4a4a4a;" xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Paskolu_Skaiciuokle.Controller">
   <center>
      <Button fx:id="Button_3" maxWidth="150.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="100.0" style="-fx-effect: dropshadow(gaussian, rgba(0, 0, 0, 0.3), 3, 0,5, 5, 5);" text="Show graph" BorderPane.alignment="CENTER">
         <font>
            <Font name="Times New Roman" size="12.0" />
         </font>
      </Button>
   </center>
</BorderPane>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="670.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Loan_calculator.LineGraphController">
 <!-- some code -->
</AnchorPane>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="670.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Loan_calculator.AnnuityGraphController">
 <-- some code -->
</AnchorPane>

提前感谢你的帮助。
P.S.这些是我试图寻找解决方案的链接,有很多不同的方法来编写这个代码,但就是找不到一个我可以实现到我的代码中…或者也许我只是缺乏如何去做的知识。不管怎样,我希望有人能帮助我或解释如何解决这个问题。链接:
•传递参数JavaFX FXML
•如何使用不同的FXML文件创建多个JavaFX控制器?
•多个带有控制器的FXML,共享对象
我的主要问题是从我的主控制器访问其他控制器。(所有控制器都链接到它们自己的FXML文件)。

共有1个答案

卢志强
2023-03-14

我不太明白你的问题,但我试着回答。我想你想从主控制器访问其他控制器,最简单的方法是:

FXMLLoader mainLoader = new FXMLLoader(getClass().getResource("MainController.fxml"));
Parent main = mainLoader.load();
MainController mainController = mainLoader.getController();

FXMLLoader otherLoader = new FXMLLoader(getClass().getResource("OtherController.fxml"));
Parent other = otherLoader.load();
// set other controller in main controller
mainController.setOtherController(otherLoader.getController());

如果使用javafx-weaver和spring boot,DI将使其变得更加容易:

@Component
@FxmlView
class MainController {
    @Autowired
    private FxControllerAndView<OtherController, VBox> otherControllerAndView;

    // otherControllerAndView.getController() to access other controller
}
 类似资料:
  • 这让我的头撞到了墙上!!!!希望有人能给我指出正确的方向。我确信我在做一些完全愚蠢的事情。我找了又找,但似乎找不出为什么这不管用!!(在IntelliJ IDE 15.x上使用JDK8.x和Scenebuilder)。我试图在GUI上显示数据,但希望access通过编程将这些数据从其他类/方法发送给它。。。。下面是一个简单的概念,我在着手更大的项目之前,正试图让它发挥作用: 我的简单GUI在FXM

  • 我尝试使用这个存储库https://github.com/ctongfei/progressbar为我的光线跟踪添加进度条。当我运行测试时,我得到很多异常。 我完全不知道它们是什么意思,也不知道如何开始修复它们。 我的测试代码: 我收到的错误消息: 2019年7月15日9:14:57PM org . jline . utils . log logr警告:无法检索哑色Java . io . io类型

  • 我正在学习使用SceneBuilder创建一个新的JavaFX应用程序的教程,并尝试向.fxml文件添加一个controller类。 我正在使用Eclipse和Gluon的Scene Builder V8.1.1版本。 在哪里可以将我的.fxml文件连接到我的控制器Java类?

  • 问题内容: 我的图像是我试图将数据(变量)从一个阶段传递到另一阶段,但是当我尝试在第二阶段访问它们时,它们为空。mainWindow的代码。前往window1 // window1类: 如果我想访问以前收到的标题,则为null 初始化显示应该为“ Window1”时显示为null} 问题答案: 在加载FXML文件的过程中,将调用该方法-换句话说,在调用时将调用该方法。 显然,这是 在 调用 之前

  • 我正试图从这个js发布我的数据 有什么建议如何修复它吗? 堆栈跟踪:

  • 问题内容: 我在设计要开发的应用程序体系结构时遇到了一些麻烦。我正在研究JAVA,因此开始研究此应用程序是因为我想加深对JAVA,体系结构和模式的整体了解。我想遵循指导方针来制作可复用的,低耦合的应用程序,就像应该的那样。该应用程序只有一个JFrame,但内部有几个JPanel,每个JPanel代表应用程序的一个模块。 问题是:在JAVA Swing中,如何实现适当的MVC模式?我在如何理解应该完