JavaFX:对话框 Alert

JavaFX:对话框 Alert


avaFX 8中的  java.scene.conrtol.Alert类可以用于显示对话框,大致上有4种常用的类型:Information,Comfiramtion,Error,Warning;
消息窗口(Information)
JavaFX:对话框 Alert_第1张图片
 
     
  1. Alert information = new Alert(Alert.AlertType.INFORMATION,"Welocme to JavaFX");
  2. information.setTitle("information"); //设置标题,不设置默认标题为本地语言的information
  3. information.setHeaderText("Information"); //设置头标题,默认标题为本地语言的information
  4. Button infor = new Button("show Information")
  5. infor.setOnAction((ActionEvent)->{
  6. information.showAndWait(); //显示弹窗,同时后续代码等挂起
  7. }) ;
确认窗口(Confirmation)
JavaFX:对话框 Alert_第2张图片
 
     
  1. Alert confirmation = new Alert(Alert.AlertType.CONFIRMATION,"exit?");
  2. Button confir = new Button();
  3. confir.setOnAction((ActionEvent)->{
  4. Optional<ButtonType> result = confirmation.showAndWait();
  5. if(result.isPresent() && result.get() == ButtonType.OK){
  6. System.exit(0);
  7. }
  8. /其他Optional<ButtonType>参数:ButtonType.CANCEL
  9. }) ;
错误窗口(Error)
JavaFX:对话框 Alert_第3张图片
 
      
  1. Alert error = new Alert(Alert.AlertType.ERROR,"ERROR: NUll Object Exception");
  2. Button err = new Button();
  3. err.setOnAction((ActionEvent e)->{
  4. error.showAndWait();
  5. });
警告窗口(Warning)
JavaFX:对话框 Alert_第4张图片
 
       
  1. Alert warning = new Alert(Alert.AlertType.WARNING,"Warning:Oh,it doesn't matter,for warning");
  2. Button warn = new Button();
  3. warn.setOnAction((ActionEvent e)->{
  4. warning.showAndWait();
  5. });


你可能感兴趣的:(Java,JavaFX)