包含文字、图片、自定义按钮、折叠和展开
基本弹框、确认框等等
自定义登录对话框
package application;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
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.ChoiceDialog;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Pair;
public class MyAnchorPane extends Application{
@Override
public void start( Stage primaryStage) throws Exception {
/**
AnchorPane anchorPane=new AnchorPane();
anchorPane.setPrefSize(300, 300);
anchorPane.setStyle("-fx-background-color:#1E90FF;");
Button button=new Button ("button");
AnchorPane.setLeftAnchor(button, 10.0);
AnchorPane.setTopAnchor(button, 10.0);
anchorPane.getChildren().add(button);
Label label = new Label("Name");
label.setTextFill(Color.RED);
label.setStyle("-fx-background-color:white;");
AnchorPane.setRightAnchor(label, 30.0);
anchorPane.getChildren().add(label);
Scene scene=new Scene(anchorPane);
HBox hBox = new HBox();
hBox.getChildren().add(new Button("Name"));
hBox.getChildren().add(new Button("Age"));
hBox.getChildren().add(new Button("Like"));
hBox.setPadding(new Insets(30,30, 30, 30));
Scene scene=new Scene(hBox);
HBox hBox = new HBox();
hBox.getChildren().add(new Button("Name"));
hBox.getChildren().add(new Button("Age"));
hBox.getChildren().add(new Button("Like"));
hBox.setPadding(new Insets(30,30, 30, 30));
VBox vBox = new VBox();
vBox.getChildren().add(new Button("Name"));
vBox.getChildren().add(new Button("Age"));
vBox.getChildren().add(new Button("Like"));
vBox.getChildren().add(hBox);
vBox.setPadding(new Insets(30,30, 30, 30));
Scene scene=new Scene(vBox);
*/
Stage stage = new Stage();
DialogPane dialogPane = new DialogPane();
dialogPane.setHeaderText("HeaderText");
dialogPane.setContentText("ContentText");
ImageView imageView = new ImageView("image\\xxb.png");
imageView.setFitWidth(50);
imageView.setPreserveRatio(true);
dialogPane.setGraphic(imageView);
Text text1 = new Text("ExpandableContentExpandableContent");
Text text2 = new Text("ExpandableContentExpandableContent");
text1.setFill(Paint.valueOf("#ccc"));
text1.setFont(Font.font(20));
VBox vBox = new VBox();
vBox.getChildren().add(text1);
vBox.getChildren().add(text2);
dialogPane.setExpandableContent(vBox);
dialogPane.setExpanded(true);
dialogPane.getButtonTypes().add(ButtonType.OK);
dialogPane.getButtonTypes().add(ButtonType.APPLY);
dialogPane.getButtonTypes().add(ButtonType.CANCEL);
Button btnOk = (Button) dialogPane.lookupButton(ButtonType.OK);
Button btnApply = (Button) dialogPane.lookupButton(ButtonType.APPLY);
Button btnCancel = (Button) dialogPane.lookupButton(ButtonType.CANCEL);
btnOk.setOnAction((e)->{
//确认对话框
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("Look, an Information Dialog");
alert.setContentText("I have a great message for you!");
alert.showAndWait();
// 警告
Alert alert2 = new Alert(AlertType.WARNING);
alert2.setTitle("Warning Dialog");
alert2.setHeaderText("Look, a Warning Dialog");
alert2.setContentText("Careful with the next step!");
alert2.showAndWait();
// 错误
Alert alert3 = new Alert(AlertType.ERROR);
alert3.setTitle("Error Dialog");
alert3.setHeaderText("Look, an Error Dialog");
alert3.setContentText("Ooops, there was an error!");
alert3.showAndWait();
//stage.close();
});
btnApply.setOnAction((e)->{
//确认、取消对话框
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Look, a Confirmation Dialog");
alert.setContentText("Are you ok with this?");
//alert.initStyle(StageStyle.UTILITY); // 不使用图标
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
// ... user chose OK
} else {
// ... user chose CANCEL or closed the dialog
}
//自定义确认对话框
Alert alert2 = new Alert(AlertType.CONFIRMATION);
alert2.setTitle("Confirmation Dialog with Custom Actions");
alert2.setHeaderText("Look, a Confirmation Dialog with Custom Actions");
alert2.setContentText("Choose your option.");
ButtonType buttonTypeOne = new ButtonType("One");
ButtonType buttonTypeTwo = new ButtonType("Two");
ButtonType buttonTypeThree = new ButtonType("Three");
ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
alert2.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree, buttonTypeCancel);
Optional<ButtonType> result2 = alert2.showAndWait();
if (result2.get() == buttonTypeOne){
// ... user chose "One"
} else if (result2.get() == buttonTypeTwo) {
// ... user chose "Two"
} else if (result2.get() == buttonTypeThree) {
// ... user chose "Three"
} else {
// ... user chose CANCEL or closed the dialog
}
});
btnCancel.setOnAction((e)->{
//可输入内容
TextInputDialog dialog = new TextInputDialog("walter");
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("Look, a Text Input Dialog");
dialog.setContentText("Please enter your name:");
// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
//如果用户点击了取消按钮result.isPresent()将会返回false
if (result.isPresent()){
System.out.println("Your name: " + result.get());
}// The Java 8 way to get the response value (with lambda expression).result.ifPresent(name -> System.out.println("Your name: " + name));
//可选择对话框
List<String> choices = new ArrayList<>();
choices.add("a");
choices.add("b");
choices.add("c");
ChoiceDialog<String> dialog2 = new ChoiceDialog<>("b", choices);
dialog2.setTitle("Choice Dialog");
dialog2.setHeaderText("Look, a Choice Dialog");
dialog2.setContentText("Choose your letter:");
// Traditional way to get the response value.
Optional<String> result2 = dialog2.showAndWait();
if (result2.isPresent()){
System.out.println("Your choice: " + result2.get());
}// The Java 8 way to get the response value (with lambda expression).result.ifPresent(letter -> System.out.println("Your choice: " + letter));
// 自定义登录
// Create the custom dialog.
Dialog<Pair<String, String>> dialog3 = new Dialog<>();
dialog3.setTitle("Login Dialog");
dialog3.setHeaderText("Look, a Custom Login Dialog");
// Set the icon (must be included in the project).
dialog3.setGraphic(new ImageView(this.getClass().getResource("/image/xxb.png").toString()));
// Set the button types.ButtonType login
ButtonType loginButtonType= new ButtonType("Login", ButtonData.OK_DONE);
dialog3.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField username = new TextField();
username.setPromptText("Username");
PasswordField password = new PasswordField();
password.setPromptText("Password");
grid.add(new Label("Username:"), 0, 0);
grid.add(username, 1, 0);
grid.add(new Label("Password:"), 0, 1);
grid.add(password, 1, 1);
// Enable/Disable login button depending on whether a username was entered.Node
Node loginButton = dialog3.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
username.textProperty().addListener((observable, oldValue, newValue) -> {
loginButton.setDisable(newValue.trim().isEmpty());
});
dialog3.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(() -> username.requestFocus());
// Convert the result to a username-password-pair when the login button is clicked.
dialog3.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
new Pair<>(username.getText(), password.getText());
}
return null;
});
Optional<Pair<String, String>> result3 = dialog3.showAndWait();
result3.ifPresent(usernamePassword -> {
System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
});
});
stage.initOwner(primaryStage); //如果指定所有者或拥有者为null,那么它是一个顶级的、未拥有的对话框。
stage.initModality(Modality.WINDOW_MODAL);//可以指定对话框的模式,包括Modality.NONE、WINDOW_MODAL或Modality.APPLICATION_MODAL。
stage.setResizable(false);
stage.setScene(new Scene(dialogPane));
primaryStage.show();
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}