在JavaFX (11)中,我试图在主窗口上方显示一个警告窗口,但我不希望主窗口可点击。为此,我认为我必须更改该警报窗口的initModality (https://docs.oracle.com/javafx/2/api/javafx/stage/Stage.html参见'Modality')。它不工作是因为警告窗口的所有者为空吗?
这是我的控制器类。主类只是由IntelliJ IDEA生成的默认JavaFX主类,而.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,如果这很重要的话。
发布于 2019-05-20 01:09:18
我通过将警报代码放在Platform.runLater()
方法中修复了它,如下所示:
public void initialize(){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.initModality(Modality.APPLICATION_MODAL);
alert.setHeight(200);
alert.setWidth(300);
Platform.runLater(alert::show());
button.setOnAction(push -> {
System.out.println("button pressed");
});
}
主窗口现在不是交互式的,正如我所希望的那样。
https://stackoverflow.com/questions/56209941
复制相似问题