gpt4 book ai didi

java - 如何在 JavaFX 中使用 FXML 创建自定义对话框?

转载 作者:行者123 更新时间:2023-11-30 11:25:51 26 4
gpt4 key购买 nike

如何在 JavaFX 中使用 FXML 创建自定义对话框?

在网上的样本中,我看到的大多是这样的

@Override
public void start(Stage stage) throws Exception {
Parent root =
FXMLLoader.load(
getClass().getResource( getClass().getSimpleName() + ".fxml" ));
Scene scene = new Scene(root);

FXML 从应用程序 start() 中加载并构建根节点。

但是如果我扩展 Stage 呢?从哪里加载 FXML?在构造函数中?或者在 initStyle() 中?或者用其他方法?

最佳答案

您可以在主类中使用以下代码:

FXMLLoader loader = new FXMLLoader(getClass().getResource("Sample.fxml"));
Parent root = (Parent)loader.load();

//Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

SampleController controller = (SampleController)loader.getController();
controller.setStageAndSetupListeners(stage);

在此之后,在 SampleController 中创建一个函数 setStageAndSetupListeners(),它将接受您的舞台,现在您可以轻松使用它。

关于java - 如何在 JavaFX 中使用 FXML 创建自定义对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20107039/

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