javafx弹出窗口

来源:互联网 发布:如何查看mac版本 编辑:程序博客网 时间:2024/06/05 05:36

新建两个类,Main和AlertBox

import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.Label;import javafx.scene.layout.AnchorPane;import javafx.scene.layout.StackPane;import javafx.scene.layout.VBox;import javafx.stage.Stage;public class Main extends Application{    @Override    public void start(Stage primaryStage) {        Button button = new Button();        button.setText("Open a window");        button.setOnAction(e -> new AlertBox().display("title", "message"));        AnchorPane layout = new AnchorPane();        layout.getChildren().add(button);        Scene scene=new Scene(layout,300,300);        primaryStage.setScene(scene);        primaryStage.show();    }    public static void main(String[] args) {        launch(args);    }}
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;public class AlertBox {    public void display(String title , String message){    Stage window = new Stage();    window.setTitle("title");    //modality要使用Modality.APPLICATION_MODEL    window.initModality(Modality.APPLICATION_MODAL);    window.setMinWidth(300);    window.setMinHeight(150);    Button button = new Button("Close the window");    button.setOnAction(e -> window.close());    Label label = new Label(message);    VBox layout = new VBox(10);    layout.getChildren().addAll(label , button);    layout.setAlignment(Pos.CENTER);    Scene scene = new Scene(layout);    window.setScene(scene);    //使用showAndWait()先处理这个窗口,而如果不处理,main中的那个窗口不能响应    window.showAndWait();    }}

运行一下就是这样哒

0 0
原创粉丝点击