代码之家  ›  专栏  ›  技术社区  ›  Zephyr

未修饰的警报在请求时不会关闭

  •  0
  • Zephyr  · 技术社区  · 6 年前

    Alert 当一个长时间运行的任务正在后台完成时,我正在尝试显示。

    但是,当任务完成时,我想关闭警报。但是,我也不能通过打电话来关闭警报 close() hide() .

    此MCVE不包括背景 Task

    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.scene.control.Alert;
    import javafx.stage.Stage;
    import javafx.stage.StageStyle;
    
    public class AlertClosing extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            Alert simpleAlert = new Alert(Alert.AlertType.NONE);
    
            simpleAlert.setContentText("Testing");
            simpleAlert.initStyle(StageStyle.UNDECORATED);
    
            simpleAlert.show();
    
            // None of these seem to have any effect.
            simpleAlert.close();
            simpleAlert.hide();
            Platform.runLater(simpleAlert::close);
        }
    }
    

    我在这里找到了一些关于 警觉的 Dialog 警觉的 根本没有按钮;它只是用来在后台任务运行时显示消息。

    对话 结束规则:

    两种情况:

    • 当对话框只有一个按钮时,或
    • 当对话框有多个按钮时,只要其中一个满足以下要求之一:
      • 按钮有一个ButtonType,当调用ButtonBar.ButtonDa.isCancelButton()时,ButtonBar.ButtonDa返回true。

    关闭请求,在用户单击 对话框的对话框窗格区域中的可用按钮。

    所以这也表明 关闭() 由于在 Alert(AlertStyle.NONE) ,对吧?

    1 回复  |  直到 6 年前
        1
  •  2
  •   fabian    6 年前

    不,根据javadoc不接受关闭请求:既没有一个也没有多个按钮。

    你可以指定一个 ButtonType 因此 Alert 关好。

    Alert simpleAlert = new Alert(Alert.AlertType.NONE);
    
    simpleAlert.setContentText("Testing");
    simpleAlert.initStyle(StageStyle.UNDECORATED);
    
    simpleAlert.show();
    
    new Thread(() -> {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
        }
        Platform.runLater(() -> {
            simpleAlert.setResult(ButtonType.CANCEL);
            simpleAlert.close();
        });
    }).start();