弹窗类
package cn.zxl.AlertWindow;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
* @Description: //TODO 弹出窗口、消息对话框
* @Author: zhangxueliang
* @Create: 2021-05-27 09:50
* @Version: 1.0
**/
public class AlertWindow {
private static boolean res;
public static boolean display(String title,String msg){
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
Label label = new Label();
label.setText(msg);
Button btn1 = new Button("是");
Button btn2 = new Button("否");
btn1.setOnMouseClicked(event -> {
res=true;
System.out.println("你点击了是");
stage.close();
});
btn2.setOnMouseClicked(event -> {
res=false;
System.out.println("你点击了否");
stage.close();
});
VBox vBox = new VBox();
vBox.getChildren().addAll(label,btn1,btn2);
//设置居中
vBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vBox,200,200);
stage.setScene(scene);
stage.setTitle(title);
stage.showAndWait();
return res;
}
}
弹窗启动类
package cn.zxl.AlertWindow;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* @Description: //TODO 主类
* @Author: zhangxueliang
* @Create: 2021-05-27 09:50
* @Version: 1.0
**/
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Button btn = new Button("弹出窗口");
btn.setOnMouseClicked(event -> {
System.out.println(AlertWindow.display("新窗口", "是否关闭窗口"));
});
VBox vBox = new VBox();
vBox.getChildren().add(btn);
//设置居中显示
vBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vBox, 400, 400);
primaryStage.setTitle("弹出窗口示例");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
最终效果
单击弹出窗口
按钮会弹出新窗口。
弹窗类
同上。
消息对话框启动类
package cn.zxl.AlertWindow;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* @Description: //TODO 消息提示框
* @Author: zhangxueliang
* @Create: 2021-05-27 09:50
* @Version: 1.0
**/
public class Main2 extends Application {
Stage stage;
@Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
//单击系统自带的关闭按钮时也要弹出询问窗口
stage.setOnCloseRequest(event -> {
//点击了否,也会关闭窗口,所以要取消默认事件,单击否按钮不会关闭窗口
event.consume();
closeWindow();
});
Button btn = new Button("关闭窗口");
btn.setOnMouseClicked(event -> closeWindow());
VBox vBox = new VBox();
vBox.getChildren().add(btn);
//设置居中显示
vBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vBox, 400, 400);
stage.setTitle("弹出窗口示例");
stage.setScene(scene);
stage.show();
}
/**
* //TODO 如果点击了是按钮,就关闭所有窗口
* @Description:
* @Create: 2021/5/27 10:59
* @Author: zhangxueliang
* @Param:
* @Return:
*/
private void closeWindow() {
boolean b = AlertWindow.display("新窗口", "是否关闭窗口");
if (b) {
stage.close();
}
}
public static void main(String[] args) {
launch(args);
}
}
最终效果
单击是关闭窗口,单击否不关闭。
并且单击X
按钮效果一样。