Javafx 8 如何在JavaFX8中管理多个屏幕
我是JavaFX新手,有一些问题 假设我有两个fxml文件,其中有一个对应的控制器类。 每个fxml上都有一个按钮,可以打开另一个屏幕并传递一个参数 有人能举个例子说明这是怎么做到的吗,谷歌没有任何帮助。你说的“屏幕”是指JavaFXStage实例,对吗?如果是这样,那就相当简单了:Javafx 8 如何在JavaFX8中管理多个屏幕,javafx-8,Javafx 8,我是JavaFX新手,有一些问题 假设我有两个fxml文件,其中有一个对应的控制器类。 每个fxml上都有一个按钮,可以打开另一个屏幕并传递一个参数 有人能举个例子说明这是怎么做到的吗,谷歌没有任何帮助。你说的“屏幕”是指JavaFXStage实例,对吗?如果是这样,那就相当简单了: 加载第二个fxml文件 通过FXML加载程序获取控制器实例 通过在控制器上调用setter方法来传递“参数” 唯一有点不寻常的事情是获取控制器引用。您需要创建FXMLoader的实例。它不适用于通常称为静态方法:
该示例使用JavaFX属性在控制器中保存“参数”-因此,值及其更改很容易观察到,并且该属性可以绑定到任何其他字符串属性 看一看,我猜你的谷歌搜索结果是:,但你无法从中找出该做什么。这只有一个屏幕(MainForm.fxml)。您是否可以添加第二个屏幕(另一个fxml)并打开它(例如,单击按钮后)。这就是我被困的地方。很抱歉最近回来了-我现在太忙了。。。稍后我将粘贴一些代码,但是:如果您想启动一个新窗口,它或多或少是来自start()方法的代码-方法:将其放入按钮的操作处理程序方法中,并创建一个新的Stage实例,而不是注入Stage参数的实例。
@FXML
public void handleAddPartAction(ActionEvent event) throws IOException {
Stage stage;
Parent root;
//get reference to the button's stage
stage=(Stage) partAddButton.getScene().getWindow();
//load up OTHER FXML document
root = FXMLLoader.load(getClass().getResource("AddPart.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
public class MainFormController implements Initializable {
// some ui control:
@FXML
private Label label;
// JavaFX property for values that shall be accessible from outside:
private final StringProperty text = new SimpleStringProperty();
public String getText() {
return text.get();
}
public void setText(String value) {
text.set(value);
}
public StringProperty textProperty() {
return text;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println("MainFormController.initialize");
this.label.textProperty().bind(this.text);
}
// ...
@FXML
public void handleAddPartAction(ActionEvent event) throws IOException {
Stage stage;
Parent root;
//get reference to the button's stage
stage=(Stage) partAddButton.getScene().getWindow();
//load up OTHER FXML document
root = FXMLLoader.load(getClass().getResource("AddPart.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();