如何在JavaFX中创建警报?
以下示例演示了 警报的创建。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class AlertExample extends Application {
public void start(Stage stage) {
//Creating a dialog
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
//Setting the title
alert.setTitle("Alert");
ButtonType type = new ButtonType("Ok", ButtonData.OK_DONE);
//Setting the content of the dialog
alert.setContentText("This is a confirmmation alert");
//Adding buttons to the dialog pane
alert.getDialogPane().getButtonTypes().add(type);
//Setting the label
Text txt = new Text("Click the button to show the dialog");
Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
txt.setFont(font);
//Creating a button
Button button = new Button("Show Dialog");
//Showing the dialog on clicking the button
button.setOnAction(e -> {
alert.showAndWait();
});
//Creating a vbox to hold the button and the label
HBox pane = new HBox(15);
//Setting the space between the nodes of a HBox pane
pane.setPadding(new Insets(50, 150, 50, 60));
pane.getChildren().addAll(txt, button);
//Creating a scene object
Scene scene = new Scene(new Group(pane), 595, 300, Color.BEIGE);
stage.setTitle("Alert");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出结果
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练