简体   繁体   English

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

[英]How to create custom dialog with FXML in JavaFX?

How to create custom dialog with FXML in JavaFX? 如何在JavaFX中使用FXML创建自定义对话框?

In samples over the Net I see mostly something like this 在网上样本中,我大多看到这样的东西

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

ie FXML is loaded from within application start() and builds root node. FXML从应用程序start()内加载并构建根节点。

But what if I extend Stage? 但是,如果我延长舞台怎么办? Where to load from FXML? 从FXML加载到哪里? In constructor? 在构造函数中? Or in initStyle() ? 还是initStyle() Or in some other method? 还是采用其他方法?

You may use the below code in your main Class : 您可以在主类中使用以下代码:

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); 

After this in SampleController Make a function setStageAndSetupListeners(), which will accept your stage and now you use it easily. 之后,在SampleController中创建一个函数setStageAndSetupListeners(),该函数将接受您的舞台,现在您可以轻松使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM