java - 打开弹窗时程序闪烁

标签 java javafx

创建了一个全屏应用程序,在按下 ESCAPE 键时会打开一个弹出窗口。一旦运行,应用程序就会闪烁,然后显示弹出窗口。

    //Pop-up window
    Stage window = new Stage();
    window.initModality(Modality.NONE);
    //Exit Panel
    VBox exitBox = new VBox();
    exitBox.setPadding(new Insets(10));
    Button exitPaneExit = new Button();
    exitPaneExit.setText("Return");
    exitPaneExit.setMinSize(75.0, 30.0);
    exitPaneExit.setOnAction(e -> {
        window.close();
    });
    Button exitButton = new Button();
    exitButton.setText("Exit");
    exitButton.setMinSize(75.0, 30.0);
    exitButton.setOnAction(e -> {
        System.exit(0);
    });
    exitBox.getChildren().addAll(exitPaneExit,exitButton);
    exitBox.setVisible(true);
    Scene scene = new Scene(exitBox);
    window.initStyle(StageStyle.UNDECORATED);
    window.initOwner(primaryStage);
    window.setScene(scene);
    mapScene.setOnKeyPressed(e -> {
        if(e.getCode()==KeyCode.ESCAPE)
        {
            window.show();
        }
    });

代码运行良好,我没有收到任何错误,但是应用程序在打开弹出窗口时闪烁,这非常烦人。

最佳答案

为什么需要创建另一个舞台?不能使用像这样的简单自定义对话框吗?

Dialog window = new Dialog();
window.setTitle("Your title");
window.setHeaderText("Your header");
window.setContentText("Your Content Text")
window(Modality.APPLICATION_MODAL);
window(mainStage);
window(StageStyle.UTILITY);
ButtonType btnReturn = new ButtonType("return");
ButtonType btnExit = new ButtonType("exit");
window.getDialogPane().getButtonTypes().addAll(btnReturn, btnExit);
mapScene.setOnKeyPressed(e -> {
    if(e.getCode()==KeyCode.ESCAPE){
        Optional<ButtonType> result = window.showAndWait();
        if (result.get() == btnExit){
            Platform.exit();
            System.exit(0);
        }
    }
});

我使用 main 方法将阶段从类传递到 Controller ,因此它不应生成任何异常,如下所示:

主类:

public void start(Stage stage) throws Exception {

    FXMLLoader loader = new FXMLLoader(getClass().getResource("ToolConfiguration.fxml"));

    Parent root = (Parent)loader.load();

    ToolConfigurationController controller = loader.<ToolConfigurationController>getController();
    controller.setLanguage("en", stage);

    notifier = Notification.Notifier.INSTANCE;
    [...]
}

对我来说很好用

在 Controller 类中是这样的:

public void setLanguage(String language, Stage stage){
    this.mainStage = stage;
    this.language=language;

    locale = new Locale(language.toLowerCase(), language);
    bundle = ResourceBundle.getBundle("smartDrawerTool.bundles.ToolConfiguration", locale);
}

关于java - 打开弹窗时程序闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39380956/

相关文章:

java - 在tomcat中设置系统属性

java - jvm中的异步异常

java - Fxml 文件未在 Eclipse 中更新

java - 如何从具有重复条目的过滤列表中删除特定索引?

java - 将 JavaFX 图像转换为 BufferedImage

java - 使用 junit 测试同步 api

java - 从具有对象 "Field"的私有(private)字段获取值。 java

java - JTabbedPane ArrayIndexOutOfBoundsException : 0

java - 触摸 ARM 上的 JavaFX(Java 1.0.8-b123,VM 25.0-b70)

JavaFX 下拉按钮