普通alert确认对话框
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "确定要关闭窗口吗?", ButtonType.YES, ButtonType.NO);alert.setHeaderText(null);alert.initOwner(primaryStage);Optional<ButtonType> result = alert.showAndWait();//当点击yes的时候 输出结果:是,当点击no的时候 输出结果:否System.out.println(result.get().getText());//当点击yes的时候 输出结果:YES,当点击no的时候 输出结果:NOSystem.out.println(result.get().getButtonData().toString());//当点击yes的时候 输出结果:True,当点击no的时候 输出结果:TrueSystem.out.println(result.isPresent());//当点击yes的时候 输出结果:YES,当点击no的时候 输出结果:NOSystem.out.println(result.orElse(ButtonType.NO).getText());if("YES".equals(result.get().getText())){//点击是的时候}else{//点击否的时候}
自动定时消失
final Alert[] alert = new Alert[1];Platform.runLater(new Runnable() {@Overridepublic void run() {alert[0] = new Alert(Alert.AlertType.CONFIRMATION, "确定要关闭窗口吗?", ButtonType.YES, ButtonType.NO);alert[0].setHeaderText(null);alert[0].initOwner(primaryStage);Optional<ButtonType> result = alert[0].showAndWait();if("YES".equals(result.get().getText())){//点击是的时候new propertyUtil(2).set("wode_pid","no");}}});new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 10; i++) {try {Thread.sleep(1000);alert[0].setTitle((10-i)+"秒后关闭");alert[0].setContentText((10-i)+"秒后关闭1");} catch (InterruptedException e) {e.printStackTrace();}}Platform.runLater(new Runnable() {@Overridepublic void run() {alert[0].close();}});}}).start();