【发布时间】:2019-10-06 04:11:47
【问题描述】:
在 JavaFX (11) 中,我试图在主窗口上方显示一个警报窗口,但我不希望主窗口可点击。为此,我认为我必须更改该警报窗口的 initModality(https://docs.oracle.com/javafx/2/api/javafx/stage/Stage.html 参见“Modality”)。是不是因为警告窗口的所有者为空而不起作用?
这是我的控制器类。主类只是 IntelliJ IDEA 生成的默认 JavaFX Main 类,而 .fxml 文件只是一个带有用于测试目的的按钮的锚窗格。
package sample;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.stage.Modality;
public class Controller {
public Button button;
public void initialize(){
Alert alert = new Alert(Alert.AlertType.ERROR);
System.out.println(alert.getOwner()); //output: null
alert.initModality(Modality.APPLICATION_MODAL);
alert.setHeight(200);
alert.setWidth(300);
alert.show();
button.setOnAction(push -> System.out.println("button pressed")); //button is still pressable
}
}
主窗口中的按钮仍然可以按下,因此 initModality 不起作用。我也试过Modality.WINDOW_MODAL和Modality.NONE,没有变化。
如果这很重要,我在 Linux 上使用 bspwm。
【问题讨论】:
-
我认为这是因为您的
Alert最终显示在您的另一个窗口之前。 -
是不是因为我在initialize()方法中运行了代码?如何确保在我的应用程序启动后立即打开警报窗口?