javafx弹出窗口

javafx弹出窗口_第1张图片

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();
    }
}

javafx弹出窗口_第2张图片

你可能感兴趣的:(javafx)