警报对话框中的Java FX自定义图片

时间:2016-07-31 06:53:00

标签: java intellij-idea javafx

我是JavaFx的新手,而我正在尝试做的是创建一个带有自定义图片的提醒对话框。

首先,我正在使用IntelliJ idea,我不明白在项目中找到.jpg文件的位置(参见附图)。

其次,将图片添加到项目后,我该如何处理? picture

 Alert alert = new Alert(Alert.AlertType.ERROR, "Error", ButtonType.OK);
        alert.showAndWait();

谢谢

3 个答案:

答案 0 :(得分:4)

在IntelIJ中包含资源已成为多个问题的主题,我不会再在这里回答。请阅读有关此主题的其他问题的答案之一,例如: maba's answer to adding resources in intellij for java project

ImageViewDialog设置Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/Emojione_1F62D.svg/64px-Emojione_1F62D.svg.png"); ImageView imageView = new ImageView(image); alert.setGraphic(imageView); alert.showAndWait(); 可以使用自定义图片:

Image image = new Image(getClass().getResource("/path/to/resource/image.jpg").toExternalForm());

请注意,此示例使用来自网络的图片。对于添加为资源的图像,您可以使用类似

的内容
<label class="checkbox-inline"><input type="checkbox" id="checked" onclick="check();"></label>

        <script type="text/javascript">
        function check() {
          if($("#checked").is(":checked")){
             alert("Thanks for Attending");
            $(this).attr('disabled','disabled');
            }
          }
    }
    </script>

代替。

答案 1 :(得分:0)

本教程可以为您提供更好的解决方案。javafx 8 dialogs

或者您也可以使用以下代码

protected Alert createAlert(){
    Alert alert = new Alert(AlertType.WARNING);

    alert.initModality(Modality.APPLICATION_MODAL);
    alert.initOwner(stage);
    alert.getDialogPane().setContentText("Some text");

    DialogPane dialogPane = alert.getDialogPane();
    GridPane grid = new GridPane();
    ColumnConstraints graphicColumn = new ColumnConstraints();
    graphicColumn.setFillWidth(false);
    graphicColumn.setHgrow(Priority.NEVER);
    ColumnConstraints textColumn = new ColumnConstraints();
    textColumn.setFillWidth(true);
    textColumn.setHgrow(Priority.ALWAYS);
    grid.getColumnConstraints().setAll(graphicColumn, textColumn);
    grid.setPadding(new Insets(5));

    Image image1 = new Image("http://www.mcaprojecttraining.com/images/java-big-icon.png");
    ImageView imageView = new ImageView(image1);
    imageView.setFitWidth(64);
    imageView.setFitHeight(64);
    StackPane stackPane = new StackPane(imageView);
    stackPane.setAlignment(Pos.CENTER);
    grid.add(stackPane, 0, 0);

    Label headerLabel = new Label("Warning");
    headerLabel.setWrapText(true);
    headerLabel.setAlignment(Pos.CENTER_RIGHT);
    headerLabel.setMaxWidth(Double.MAX_VALUE);
    headerLabel.setMaxHeight(Double.MAX_VALUE);
    grid.add(headerLabel, 1, 0);

    dialogPane.setHeader(grid);
    dialogPane.setGraphic(null);

    alert.showAndWait()
        .filter(response -> response == ButtonType.OK)
        .ifPresent(response -> System.out.println("The alert was approved"));

    return alert;
}

资源链接:

  1. Set image on left side of dialog
  2. How do you set the icon of a Dialog control Java FX/Java 8

答案 2 :(得分:0)

首先添加GridPane

        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(10, 10, 10, 10));
        ImageView customImage = new ImageView(new Image("/home/mani/Desktop/img.jpg", 100, 80, false, true));
        grid.add(customImage, 0, 0);
        // make a dialog 
        final Dialog dlg = new Dialog(null, "myDailog-with-Image");
        // Add grid inside dialog.
        dlg.setContent(grid);
        dlg.show();