代码之家  ›  专栏  ›  技术社区  ›  shehan96

如何更改警报对话框的图标?

  •  3
  • shehan96  · 技术社区  · 6 年前

    这就是我想要改变的:

    Screenshot

    我想更改图标。意思是我想把那个蓝色图标改成别的东西。不更改alret类型

    2 回复  |  直到 6 年前
        1
  •  6
  •   Zephyr    6 年前

    你有几个选择。

    首先 Alert 类接受 AlertType 创建警报时的参数。有5个内置选项可供选择,每个选项都有自己的图标:

    INFORMATION CONFIRMATION , WARNING , ERROR ,和 NONE

    您可以在创建 通过 警报类型 致施工单位:

    Alert alert = new Alert(AlertType.ERROR);

    ERROR screenshot


    dialogPane 警觉的 设置 graphic 属性:

    alert.getDialogPane().setGraphic(new ImageView("your_icon.png"));
    

    警觉的 :

    import javafx.application.Application;
    import javafx.scene.control.Alert;
    import javafx.scene.image.ImageView;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
    
            // Build the Alert
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Alert Test");
            alert.setHeaderText("This uses a custom icon!");
    
            // Create the ImageView we want to use for the icon
            ImageView icon = new ImageView("your_icon.png");
    
            // The standard Alert icon size is 48x48, so let's resize our icon to match
            icon.setFitHeight(48);
            icon.setFitWidth(48);
    
            // Set our new ImageView as the alert's icon
            alert.getDialogPane().setGraphic(icon);
            alert.show();
        }
    }
    

    警觉的

    Custom Icon Alert


    注: ImageView 对于图形。这个 setGraphic() 方法接受任何 Node 对象,这样你就可以很容易地通过 Button Hyperlink ,或其他UI组件。

        2
  •  6
  •   Sai Dandem    6 年前

    除了@Zephyr已经提到的以外,如果您想在屏幕截图中指向的位置设置自己的自定义图标/图形,请使用的setGraphic()方法 javafx.scene.control.Dialog

    在下面的代码中,虽然alertType是INFORMATION,但它将用提供的图形节点覆盖预定义的图标。

    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setTitle("My Title");
    alert.setContentText("My Content text");
    alert.setHeaderText("My Header");
    alert.setGraphic(new Button("My Graphic"));
    alert.show();
    

    enter image description here

    推荐文章