问题描述
我有一个包含以下文件的JavaFx应用程序:
I have a JavaFx application with following files:
- MainApp.java-负责处理应用程序的Java类
- Controller.java-对应的控制器文件
- Design.fxml-通过MainApp.java加载并由Controller.java控制的应用程序的FXML文件
现在,假设我有另一个类文件,例如Compute.java,它具有一个方法(例如doSomething()).当此方法终止时,我希望在原始FXML文件(例如,状态为工作已完成"的框)的顶部打开一个内置的Alert框或一个自定义FXML文件.
Now, let's say I have another class file as Compute.java which has a method (say doSomething()). When this method terminates, I wish to open a built-in Alert box or a custom FXML file on top of the original FXML file (say, a box which states "Work Completed").
请为此提出一个整洁的解决方案(这不涉及将Compute.java的逻辑移至任何其他文件或Controller.java.此外,我希望保持Compute.java的JavaFx代码清洁). /p>
Please suggest a neat solution for this (which does not involve moving the logic of Compute.java to any other file or to the Controller.java. Also, I wish to keep the Compute.java clean of JavaFx code).
推荐答案
建议:
由于主要的主要舞台(和场景)在MainApp中举行,
您可以将此类注入Compute
Since the main primary stage (and scene) held in MainApp,
you may inject this class into Compute
// in MainApp.java
Compute compute = new Compute();
compute.setMainApp(this);
之后,您致电
// in Compute.java
mainApp.showAlert(myTitle, myContent);
其中
// in MainApp.java
public void showAlert(String myTitle, Node myContent) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(myTitle);
alert.setHeaderText(null);
alert.getDialogPane.setContent(myContent);
alert.showAndWait();
}
// or your custom stage
public void showAlert(String myTitle, Node myContent) {
Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setScene(new Scene(new VBox(new Label(myTitle), myContent));
dialogStage.show();
}
这篇关于Javafx:从另一个Java类打开新的FXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!