[英]How to create custom dialog with FXML in JavaFX?
如何在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()
內加載並構建根節點。
但是,如果我延長舞台怎么辦? 從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(),該函數將接受您的舞台,現在您可以輕松使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.