简体   繁体   English

JavaFX - 最小化舞台的按钮

[英]JavaFX - Button to minimize the stage

I have an undecorated stage in a JavaFX Application.我在 JavaFX 应用程序中有一个未修饰的阶段。 In order to minimize it I need a minimize button.为了最小化它,我需要一个最小化按钮。 I created my layout in an fxml document which has a minimize button , but when I try to minimize using the action listener for this button located inside the controller using stage.setIconified(true) , it cannot find stage.我在一个fxml document创建了我的布局,它有一个minimize button ,但是当我尝试使用位于控制器内部的这个按钮的action listener最小化stage.setIconified(true) ,它找不到舞台。

How can I find a reference to the stage in the controller class?如何在控制器类中找到对舞台的引用?

You can try :你可以试试 :

button.setOnAction(e -> {
    ((Stage)((Button)e.getSource()).getScene().getWindow()).setIconified(true);
});

Solution number 1 is what ItachiUchiha posted.解决方案 1 是 ItachiUchiha 发布的内容。

Solution number 2 is just creating a setter-method in the controller and give the controller the stage itself.解决方案 2 只是在控制器中创建一个 setter 方法,并为控制器提供舞台本身。

Use this Stage stage = (Stage) minimize.getScene().getWindow(); stage.setIconified(true); // minimize can be any element in that scene使用这个Stage stage = (Stage) minimize.getScene().getWindow(); stage.setIconified(true); // minimize can be any element in that scene Stage stage = (Stage) minimize.getScene().getWindow(); stage.setIconified(true); // minimize can be any element in that scene

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

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