`
iaiai
  • 浏览: 2179887 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JavaFX Alert对话框

    博客分类:
  • J2EE
 
阅读更多
1. 标准对话框

消息对话框
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 alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("I have a great message for you!");

alert.showAndWait();




2. 警告对话框
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Warning Dialog");
alert.setHeaderText("Look, a Warning Dialog");
alert.setContentText("Careful with the next step!");

alert.showAndWait();




3. 错误对话框
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error Dialog");
alert.setHeaderText("Look, an Error Dialog");
alert.setContentText("Ooops, there was an error!");

alert.showAndWait();



4. 异常对话框
这不是一个完整的异常对话框。但我们可以很容易地将 TextArea 作为可扩展的内容。
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Exception Dialog");
alert.setHeaderText("Look, an Exception Dialog");
alert.setContentText("Could not find file blabla.txt!");

Exception ex = new FileNotFoundException("Could not find file blabla.txt");

// Create expandable Exception.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String exceptionText = sw.toString();

Label label = new Label("The exception stacktrace was:");

TextArea textArea = new TextArea(exceptionText);
textArea.setEditable(false);
textArea.setWrapText(true);

textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);

GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(label, 0, 0);
expContent.add(textArea, 0, 1);

// Set expandable Exception into the dialog pane.
alert.getDialogPane().setExpandableContent(expContent);

alert.showAndWait();



5. 确认对话框
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Look, a Confirmation Dialog");
alert.setContentText("Are you ok with this?");

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
    // ... user chose OK
} else {
    // ... user chose CANCEL or closed the dialog
}



6. 自定义确认对话框
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog with Custom Actions");
alert.setHeaderText("Look, a Confirmation Dialog with Custom Actions");
alert.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);

alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree, buttonTypeCancel);

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne){
    // ... user chose "One"
} else if (result.get() == buttonTypeTwo) {
    // ... user chose "Two"
} else if (result.get() == buttonTypeThree) {
    // ... user chose "Three"
} else {
    // ... user chose CANCEL or closed the dialog
}




7. 可输入的对话框
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();
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));


说明:如果用户点击了取消按钮result.isPresent()将会返回false


8. 可选择的对话框
List<String> choices = new ArrayList<>();
choices.add("a");
choices.add("b");
choices.add("c");

ChoiceDialog<String> dialog = new ChoiceDialog<>("b", choices);
dialog.setTitle("Choice Dialog");
dialog.setHeaderText("Look, a Choice Dialog");
dialog.setContentText("Choose your letter:");

// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
    System.out.println("Your choice: " + result.get());
}

// The Java 8 way to get the response value (with lambda expression).
result.ifPresent(letter -> System.out.println("Your choice: " + letter));


引用
说明:如果用户没有选择或点击了取消,result.isPresent()将会返回false



9. 自定义登录框
// Create the custom dialog.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Login Dialog");
dialog.setHeaderText("Look, a Custom Login Dialog");

// Set the icon (must be included in the project).
dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));

// Set the button types.
ButtonType loginButtonType = new ButtonType("Login", ButtonData.OK_DONE);
dialog.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 loginButton = dialog.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());
});

dialog.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.
dialog.setResultConverter(dialogButton -> {
    if (dialogButton == loginButtonType) {
        return new Pair<>(username.getText(), password.getText());
    }
    return null;
});

Optional<Pair<String, String>> result = dialog.showAndWait();

result.ifPresent(usernamePassword -> {
    System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
});



10. 修改对话框样式

自定义图标
// Get the Stage.
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();

// Add a custom icon.
stage.getIcons().add(new Image(this.getClass().getResource("login.png").toString()));


说明:根据JavaFX 8u40最终版本的BUG报告,应该使用与它正在运行的应用程序相同的图标。在这种情况下,你还需要设置它的所有者,对话框会得到所有者的图标:
dialog.initOwner(otherStage);

不使用图标
dialog.initStyle(StageStyle.UTILITY);




11. 其他操作

设置拥有者

你可以为每一个对话框指定所有者。如果指定所有者或拥有者为null,那么它是一个顶级的、未拥有的对话框。
dialog.initOwner(parentWindow);


设置模式

你可以指定对话框的模式,包括Modality.NONE、WINDOW_MODAL或Modality.APPLICATION_MODAL。
dialog.initModality(Modality.NONE);


  • 大小: 12.6 KB
  • 大小: 11 KB
  • 大小: 11.9 KB
  • 大小: 11.6 KB
  • 大小: 28.3 KB
  • 大小: 13.6 KB
  • 大小: 15.3 KB
  • 大小: 7.2 KB
  • 大小: 7.2 KB
  • 大小: 17.2 KB
  • 大小: 10.7 KB
  • 大小: 7 KB
分享到:
评论

相关推荐

    Alert消息框中设置icon图标的例子

    #### 将icon图标嵌入到Alert对话框的按钮中 除了直接在Alert消息框中嵌入图标外,还可以将图标嵌入到消息框的按钮中。这种方法可以使得按钮更加直观,增强用户体验。具体步骤如下: 1. **定义多个图标资源**: - ...

    java点击按钮弹出对话框

    1. **JavaFX的对话框**:JavaFX没有直接等价于`JOptionPane`的类,但我们可以使用`Alert`类创建简单的对话框,或者创建自定义的`Dialog`类来获取更复杂的交互。例如,创建一个警告对话框: ```java Alert alert = ...

    javafx模拟磁盘管理系统

    5. **对话框**:如需弹出确认对话框或输入对话框,可以使用JavaFX的Alert和TextInputDialog类。例如,删除文件前,可能会弹出一个Alert询问用户是否确认删除。 6. **右键菜单**:JavaFX中的ContextMenu组件可以创建...

    javafx ui controls

    4. **对话框**:如 Alert、FileChooser 等用于交互式操作的控件。 5. **自定义控件**:允许开发者根据需求创建自己的 UI 控件。 #### 三、基础控件详解 1. **Button** - Button 控件是最基本的交互控件之一,用于...

    层层调出对话框

    JavaFX提供`Alert`类来创建对话框。同样,可以使用条件语句层层调出对话框。 ```java Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("确认"); alert.setHeaderText("是否继续?"); Optional...

    弹出对话框

    例如,Visual Studio IDE为C#开发者提供了丰富的对话框组件,而Eclipse IDE为Java开发者提供了Swing和JavaFX的UI设计器。 然而,压缩包文件名称“Axis2”似乎与弹出对话框的主题不太直接相关。Axis2是Apache的一个...

    点击按钮弹出对话框...........

    3. **JavaScript**:在网页应用中,`window.alert()`, `window.confirm()`, 和 `window.prompt()`是基本的对话框函数,分别对应信息提示、确认和输入功能。 4. **Windows Forms (.NET)**:使用`MessageBox.Show()`...

    点击按钮弹出对话框代码

    最基本的有`alert()`用于显示警告信息,`prompt()`用于获取用户输入,以及`confirm()`用于请求用户确认。这些内置对话框都是阻塞式的,意味着它们会暂停页面的执行,直到用户做出回应。 ```javascript // 示例:...

    JavaFX控件和实用程序的集合.zip

    JavaFX提供了模态和非模态对话框,如Alert、Dialog,便于实现用户通知、输入验证等场景。 10. **3D支持** JavaFX支持3D图形,提供了Scene3D、Group3D等类,可以创建丰富的3D应用场景。 总结,"JavaFX控件和实用...

    打开弹出对话框代码

    在桌面应用开发中,如使用Java Swing或JavaFX,我们可以使用JOptionPane类来创建对话框: ```java import javax.swing.JOptionPane; public class PopupDialogExample { public static void main(String[] args) ...

    javaFX-sample:JavaFX项目样本

    8. **JavaFX与模态对话框**:JavaFX提供了一些内置的对话框,如Alert和FileChooser,它们可以用来提示用户信息、获取用户输入或选择文件。 9. **JavaFX打包与部署**:一个完整的JavaFX项目需要打包成可执行的JAR...

    javafxlloginandsignup_ZXF_

    可以使用JavaFX的对话框(Alert)来实现这一点。 7. **状态管理**:登录成功后,应用可能会保存用户的登录状态,以便在下次打开应用时自动登录或跳过登录页面。这涉及到会话管理,可能需要使用Singleton模式来维护...

    关于JAVA8 Window类的使用方法.zip

    JavaFX提供了Alert、FileChooser、DirectoryChooser等对话框类型。例如,创建一个信息提示对话框: ```java Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("提示信息"); alert.setHeaderText...

    基于Java的文字跑马灯与信息窗口.zip

    JavaFX的`Alert`类可以创建各种对话框,包括信息、警告、确认和错误。 ```java Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("信息窗口"); alert.setHeaderText("这是头部信息"); alert....

    java代码-alert测试

    在Java编程语言中,`alert`这个词通常与用户交互有关,特别是在Web开发中,JavaScript的`alert()`函数用于弹出一个包含消息的警告对话框。然而,在Java本身中,并没有直接的`alert`方法。标题"java代码-alert测试...

    初探Java FX一个小系统企业员工系统

    在登录过程中,我们可能会使用`Alert`对话框来显示错误消息或确认信息。Alert类提供了几种不同的类型,如警告、信息、确认和错误,可以方便地弹出提示框与用户交互。 在系统实现中,我们需要实现一个后台服务来处理...

    JavaFxTest:有关如何使JavaFx在Repl.it,Hello World和简单按钮上使用的示例文件

    这个例子展示了如何利用JavaFX的`Alert`类创建一个信息对话框,并在按钮被点击时显示。 通过这种方式,你可以逐步探索JavaFX的更多功能,如布局管理、自定义组件、动画效果、数据绑定等。在Repl.it上实践这些概念,...

    DialogSamples

    - 在Java中,Swing提供了JOptionPane类,JavaFX有Dialog类来创建不同类型的对话框。 - 在Python的GUI库如Tkinter中,Toplevel对象可以用来创建自定义对话框。 - 在Web开发中,HTML5的`&lt;dialog&gt;`元素和JavaScript...

    仿qq弹出提示框,Java语言实现

    此外,如果你的项目使用了JavaFX,可以利用`Alert`类来创建提示框,它的使用方式类似,但提供了更多的定制选项和更现代的界面。 在提供的压缩包文件`test`中,可能包含了实现这个功能的源代码。你可以通过查看和...

Global site tag (gtag.js) - Google Analytics