gpt4 book ai didi

javafx-2 - 如何将在 FXML Controller1 中创建的对象传递给内部 FXML 控件的 Controller2

转载 作者:行者123 更新时间:2023-12-03 21:19:24 24 4
gpt4 key购买 nike

我有一个 JavaFX 2.0 应用程序,它由两个 FXML 文件和两个 Controller + 一个“主”.java 文件组成。
在开始时,FXML1 被初始化,如下所示:

public void start(Stage stage) throws Exception {
stage.setTitle("Demo Jabber JavaFX Chat");

Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"),
ResourceBundle.getBundle("fxmlexample.fxml_example"));
Scene scene = new Scene(root, 226, 264);
stage.setScene(scene);
scene.getStylesheets().add("fxmlexample/fxmlstylesheet.css");
stage.show();
}
然后,当单击scene1 中的按钮时,在Controller1 类的事件处理程序中,我更改scene1 根目录,以向用户显示新的gui View 。在这个 Controller 中,我初始化了一些对象。例如,像这样:
public class FXMLExampleController {
//some fields...
private MySuperObject c;
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
//some fields...
c = new MySuperObject(); //here i initialize my object, i'm interested in
try {
c.login(username, password); // some actions with this object, which i need to make.
Scene cc = buttonStatusText.getScene();
Parent root = null;
try {
//changing a scene content...
root = FXMLLoader.load(getClass().getResource("fxml_example2.fxml"),
ResourceBundle.getBundle("fxmlexample.fxml_example"));
} catch (IOException ex) {
Logger.getLogger(FXMLExampleController.class.getName()).log(Level.SEVERE, null, ex);
}
cc.setRoot(root);
}
而且,在那之后,我必须在下一个场景中对那个对象做一些工作,它不能是同一个类的新实例,而是我在第一个场景中初始化的对象。
我了解如何使用“标准 java”来完成这些,但我对使用 JavaFX + FXML 的这项任务感到有些困惑。

最佳答案

在 FX 2.2 中引入了用于 Controller 节点的新 API:

// create class which is both controller and node
public class InnerFxmlControl extends HBox implements Initializable {
@FXML public ComboBox cb;

public InnerFxmlControl () {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml_example2.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}

使用下一个 fxml(注意标签 fx:root ):
<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml">
<children>
<ComboBox fx:id="cb" />
</children>
</fx:root>

这样,您就创建了一个新控件,可以将其用作常规 JavaFX 控件。例如。在你的情况下:
@FXML protected void handleSubmitButtonAction(ActionEvent event) {
// you just create new control, all fxml tricks are encapsulated
InnerFxmlControl root = new InnerFxmlControl();
// and you can access all its' methods and fields including matched by @FXML tag:
root.cb.getItems().add("new item");

Scene cc = buttonStatusText.getScene();
cc.setRoot(root);
}

在 fxml 中:
<InnerFxmlControl />

关于javafx-2 - 如何将在 FXML Controller1 中创建的对象传递给内部 FXML 控件的 Controller2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9717852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com