原来可调整大小的JavaFX舞台在显示模态对话框后不能再调整大小。这只发生在Linux: Ubuntu和XUbuntu。
下面的代码显示了一个带有按钮的窗口。窗口最初可以毫无问题地调整大小。点击按钮后,会显示一个警告对话框,之后窗口就不能再调整大小了。我是不是漏掉了什么?这是Ubuntu的bug吗?import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class StageTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Show Alert");
btn.setOnAction(e -> {
Alert alert = new Alert(AlertType.WARNING, "This is an alert", ButtonType.YES);
alert.showAndWait();
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 1000, 850));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是JavaFX在linux虚拟机上运行时发生的错误。我在CentOS 7上使用oracle jdk 1.8.0_152在VMWare上运行时也有同样的问题。
此问题的错误单位于这里:https://bugs.openjdk.java.net/browse/JDK-8140491
这个问题只提到了Ubuntu在Virtual Box上,但是我在VMWare上的CentOS上也遇到了同样的问题。
不幸的是,JDK bug系统不对公众开放,所以我不能投票或关注这个问题。目前,他们一直在拖延时间,不打算在Java 11之前修复这个问题!也许如果有足够多的人抱怨,他们会更快地解决这个问题。
我可以确认使用JDK 1.8u192的RHELS 6.9也会发生这种情况。
只有在警报/对话框被取消(隐藏)后,主舞台才会停止调整大小。
一个解决方法是在显示之前调用alert.initModality(Modality.NONE)
。我不知道为什么这个工作,但它修复了它为我。