如何在JavaFX的2.0创建并显示通用对话框

如题所述

/** 
    * 创建提示对话框,能选择 
    *  
    * @param title 对话框标题 
    * @param message 对话框显示的主信息 
    * @param instage 父节点 
    * @return 选择的结果 
    */ 
   public int showAlert(String title, String message, Stage instage){ 
        
       //设置总背景 
       Rectangle background = new Rectangle(320,220); 
       background.setArcHeight(15); 
       background.setArcWidth(15); 
       background.setOpacity(0.7); 
       background.setFill(Color.WHITESMOKE); 
        
       //设置标题背景 
       Rectangle headlingBackground = new Rectangle(300,50); 
       headlingBackground.setX(10); 
       headlingBackground.setY(10); 
       headlingBackground.setFill(Color.valueOf("#f4b300")); 
       headlingBackground.setOpacity(0.9); 
        
       //设置标题 
       Text heading = new Text(" "+title); 
       heading.setTextOrigin(VPos.TOP); 
       heading.setFill(Color.WHITESMOKE); 
       heading.setFont(Font.font("Arial", 25)); 
       heading.layoutXProperty().bind(headlingBackground.xProperty() ); 
       heading.layoutYProperty().bind(headlingBackground.yProperty() ); 
        
       //设置展示背景 
       Rectangle displayBackground = new Rectangle(300,150); 
       displayBackground.setX(10); 
       displayBackground.setY(60); 
       displayBackground.setFill(Color.LIGHTGRAY); 
       displayBackground.setOpacity(0.5); 
        
       //设置展示的信息 
       Text display = new Text(" "+message); 
       display.layoutXProperty().bind(displayBackground.xProperty() ); 
       display.layoutYProperty().bind(displayBackground.yProperty() ); 
       display.setFont(Font.font("Arial", 35)); 
       display.setTextOrigin(VPos.TOP); 
       display.setFill(Color.DARKCYAN); 
        
       //设置确定按钮 
       Button yes = new Button("YES"); 
       yes.setStyle("-fx-text-fill: #000000;-fx-background-color: #ffffff"); 
       yes.setLayoutX(200); 
       yes.setLayoutY(180); 
       yes.setOnAction(new EventHandler<ActionEvent>(){ 
 
           @Override 
           public void handle(ActionEvent event) { 
               result = 1; 
               instage.close(); 
               close(); 
           } 
        
       }); 
        
       //设置取消按钮 
       Button no = new Button("NO"); 
       no.setStyle("-fx-text-fill: #ffffff;-fx-background-color: #000000"); 
       no.setLayoutX(250); 
       no.setLayoutY(180); 
       no.setOnAction(new EventHandler<ActionEvent>(){ 
 
           @Override 
           public void handle(ActionEvent event) { 
               result = 2; 
               close(); 
           } 
        
       }); 
        
       //添加组件 
       Group root = new Group(); 
       root.getChildren().addAll(background, headlingBackground, 
               heading, display, displayBackground, yes, no); 
        
       //设置场景、标题、不可改变大小 
       setScene(new Scene(root, 320, 220)); 
       setTitle("Alert"); 
       setResizable(false); 
       show(); 
       return result; 
   }

以前写的

温馨提示:答案为网友推荐,仅供参考
相似回答