使用JavaFX中的默认图像创建对话框(信息/警告/错误)

使用JavaFX中的默认图像创建对话框(信息/警告/错误),javafx,icons,Javafx,Icons,我正在创建一个JavaFX对话框,并希望使用默认图标获取信息/警告/错误 在Swing中,我可以通过以下方式获取信息图标: UIManager.getIcon("OptionPane.informationIcon") 如何在JavaFX中执行同样的操作?您不需要为Info/Warning/Error创建自定义JavaFX对话框,因为JavaFX已经为您创建了 Alert类是Dialog类的子类,并支持许多预构建的对话框类型,这些类型可以轻松显示给用户以提示响应 您可以创建不同类型的警报,具体

我正在创建一个
JavaFX对话框
,并希望使用
默认图标
获取信息/警告/错误

在Swing中,我可以通过以下方式获取信息图标:

UIManager.getIcon("OptionPane.informationIcon")

如何在JavaFX中执行同样的操作?

您不需要为
Info/Warning/Error
创建自定义
JavaFX对话框,因为JavaFX已经为您创建了

Alert类是Dialog类的子类,并支持许多预构建的对话框类型,这些类型可以轻松显示给用户以提示响应

您可以创建不同类型的
警报
,具体取决于,它将嵌入必要的图像

对于
信息
警报使用:

Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("An Information Dialog");
alert.setContentText("Information Message");
alert.showAndWait();
类似地,对于
警告
警报,您可以使用

AlertType.WARNING
对于
错误
警报,您可以使用:

AlertType.ERROR

您不需要为
Info/Warning/Error
创建自定义
JavaFX对话框,因为JavaFX已经为您创建了

Alert类是Dialog类的子类,并支持许多预构建的对话框类型,这些类型可以轻松显示给用户以提示响应

您可以创建不同类型的
警报
,具体取决于,它将嵌入必要的图像

对于
信息
警报使用:

Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("An Information Dialog");
alert.setContentText("Information Message");
alert.showAndWait();
类似地,对于
警告
警报,您可以使用

AlertType.WARNING
对于
错误
警报,您可以使用:

AlertType.ERROR
这是我要的。我使用此代码制作带有默认图标的标签:

Label img = new Label();
img.getStyleClass().addAll("alert", "error", "dialog-pane");
dialog.setGraphic(img);
这是我要的。我使用此代码制作带有默认图标的标签:

Label img = new Label();
img.getStyleClass().addAll("alert", "error", "dialog-pane");
dialog.setGraphic(img);

如果你得到了这些图像的副本,你可以使用这些想法。

警报示例:

@FXML void handleHelpButton(ActionEvent event){
        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("Help");
        alert.setHeaderText("Help");
        alert.setGraphic(new ImageView(this.getClass().getResource("img/help.png").toString()));
        alert.setContentText("Place the cursor over a button for hint.");
        Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image(this.getClass().getResource("img/help.png").toString()));        

        alert.showAndWait();
    }
        ChoiceDialog<String> dialog = new ChoiceDialog<>(currentFullscreenSetting, choices);
        dialog.setTitle("Settings");
        dialog.setHeaderText("Settings");
        dialog.setContentText("Fullscreen on startup: ");
        dialog.setGraphic(new ImageView(this.getClass().getResource("img/settings.png").toString()));
        Stage stage2 = (Stage) dialog.getDialogPane().getScene().getWindow();
        stage2.getIcons().add(new Image(this.getClass().getResource("img/settings.png").toString()));
        // Traditional way to get the response value.
        Optional<String> result = dialog.showAndWait(); 

对话框示例:

@FXML void handleHelpButton(ActionEvent event){
        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("Help");
        alert.setHeaderText("Help");
        alert.setGraphic(new ImageView(this.getClass().getResource("img/help.png").toString()));
        alert.setContentText("Place the cursor over a button for hint.");
        Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image(this.getClass().getResource("img/help.png").toString()));        

        alert.showAndWait();
    }
        ChoiceDialog<String> dialog = new ChoiceDialog<>(currentFullscreenSetting, choices);
        dialog.setTitle("Settings");
        dialog.setHeaderText("Settings");
        dialog.setContentText("Fullscreen on startup: ");
        dialog.setGraphic(new ImageView(this.getClass().getResource("img/settings.png").toString()));
        Stage stage2 = (Stage) dialog.getDialogPane().getScene().getWindow();
        stage2.getIcons().add(new Image(this.getClass().getResource("img/settings.png").toString()));
        // Traditional way to get the response value.
        Optional<String> result = dialog.showAndWait(); 
ChoiceDialog dialog=newchoicedialog(当前全屏设置,选项);
对话框.setTitle(“设置”);
对话框.setHeaderText(“设置”);
setContentText(“启动时全屏显示:”);
setGraphic(新的ImageView(this.getClass().getResource(“img/settings.png”).toString());
Stage stage2=(Stage)dialog.getDialogPane().getScene().getWindow();
stage2.getIcons().add(新图像(this.getClass().getResource(“img/settings.png”).toString());
//获取响应值的传统方法。
可选结果=dialog.showAndWait();

这两个例子的这一部分都很棘手。我注意到,除非我将图像与.fxml和controller.java文件放在同一文件夹中,或者在同一文件夹中的某个文件夹中有提到的文件,否则这将不起作用。您可能需要使用文件位置。在我的示例中,我的setGraphic和getIcons图像似乎在同一个文件夹中,但它们不是

stage.getIcons().add(新建) 图像(this.getClass().getResource(“img/help.png”).toString())

我的文件结构如下所示:

计划图
css
img
计划图
img


第二个img文件夹保存stage.getIcons.add()的图像。这些图像也可能用于设置图形。我没有试过。

如果你得到了这些图片的副本,你可以使用这些想法。

警报示例:

@FXML void handleHelpButton(ActionEvent event){
        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("Help");
        alert.setHeaderText("Help");
        alert.setGraphic(new ImageView(this.getClass().getResource("img/help.png").toString()));
        alert.setContentText("Place the cursor over a button for hint.");
        Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image(this.getClass().getResource("img/help.png").toString()));        

        alert.showAndWait();
    }
        ChoiceDialog<String> dialog = new ChoiceDialog<>(currentFullscreenSetting, choices);
        dialog.setTitle("Settings");
        dialog.setHeaderText("Settings");
        dialog.setContentText("Fullscreen on startup: ");
        dialog.setGraphic(new ImageView(this.getClass().getResource("img/settings.png").toString()));
        Stage stage2 = (Stage) dialog.getDialogPane().getScene().getWindow();
        stage2.getIcons().add(new Image(this.getClass().getResource("img/settings.png").toString()));
        // Traditional way to get the response value.
        Optional<String> result = dialog.showAndWait(); 

对话框示例:

@FXML void handleHelpButton(ActionEvent event){
        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("Help");
        alert.setHeaderText("Help");
        alert.setGraphic(new ImageView(this.getClass().getResource("img/help.png").toString()));
        alert.setContentText("Place the cursor over a button for hint.");
        Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image(this.getClass().getResource("img/help.png").toString()));        

        alert.showAndWait();
    }
        ChoiceDialog<String> dialog = new ChoiceDialog<>(currentFullscreenSetting, choices);
        dialog.setTitle("Settings");
        dialog.setHeaderText("Settings");
        dialog.setContentText("Fullscreen on startup: ");
        dialog.setGraphic(new ImageView(this.getClass().getResource("img/settings.png").toString()));
        Stage stage2 = (Stage) dialog.getDialogPane().getScene().getWindow();
        stage2.getIcons().add(new Image(this.getClass().getResource("img/settings.png").toString()));
        // Traditional way to get the response value.
        Optional<String> result = dialog.showAndWait(); 
ChoiceDialog dialog=newchoicedialog(当前全屏设置,选项);
对话框.setTitle(“设置”);
对话框.setHeaderText(“设置”);
setContentText(“启动时全屏显示:”);
setGraphic(新的ImageView(this.getClass().getResource(“img/settings.png”).toString());
Stage stage2=(Stage)dialog.getDialogPane().getScene().getWindow();
stage2.getIcons().add(新图像(this.getClass().getResource(“img/settings.png”).toString());
//获取响应值的传统方法。
可选结果=dialog.showAndWait();

这两个例子的这一部分都很棘手。我注意到,除非我将图像与.fxml和controller.java文件放在同一文件夹中,或者在同一文件夹中的某个文件夹中有提到的文件,否则这将不起作用。您可能需要使用文件位置。在我的示例中,我的setGraphic和getIcons图像似乎在同一个文件夹中,但它们不是

stage.getIcons().add(新建) 图像(this.getClass().getResource(“img/help.png”).toString())

我的文件结构如下所示:

计划图
css
img
计划图
img


第二个img文件夹保存stage.getIcons.add()的图像。这些图像也可能用于设置图形。我没有试过。

那不是我想要的。我有一个自定义对话框,我想使用警报类使用的图标。啊,我误解了你的问题。我不知道加载它们的任何标准方法,但由于图像位于jfxrt.jar中,因此您可以使用路径
/com/sun/javafx/scene/control/skin/modena/dialog error.png
dialog warning.png
dialog information.png
@ItachiUchiha加载它们,感谢您将位置发布到图像中。这帮我解决了一个问题。我已经将警报图形设置为progressBar,但如果失败,我需要将图像重置为默认值。下面是我所做的:ImageView error=newImageView(this.getClass().getResource(“/com/sun/javafx/scene/control/skin/modena/dialog error.png”).toString();警报。设置图形(错误);那不是我想要的。我有一个自定义对话框,我想使用警报类使用的图标。啊,我误解了你的问题。我不知道加载它们的任何标准方法,但由于图像位于jfxrt.jar中,因此可以使用路径
/com/sun/javafx/scene/control/skin/modena/dialog error.png
拨号来加载它们