javaFx DialogPane 对话框

编程入门 行业动态 更新时间:2024-10-25 16:21:01

javaFx DialogPane <a href=https://www.elefans.com/category/jswz/34/1761322.html style=对话框"/>

javaFx DialogPane 对话框

包含文字、图片、自定义按钮、折叠和展开

基本弹框、确认框等等

自定义登录对话框

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{@Overridepublic 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()将会返回falseif (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 loginButtonType 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.NodeNode 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);}}

更多推荐

javaFx DialogPane 对话框

本文发布于:2024-03-12 02:52:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1730526.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对话框   javaFx   DialogPane

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!