【问题标题】:How do you set the icon of a Dialog control Java FX/Java 8如何设置对话框控件的图标 Java FX/Java 8
【发布时间】:2015-03-14 14:36:07
【问题描述】:

我可能遗漏了一些非常明显的东西,但我不知道如何为 Dialog 组件设置 Icon(更准确地说是 ProgressDialog)。我知道如何为舞台做到这一点:

this.primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/icon/Logo.png")));

但我没有为 Dialog 家族找到任何东西。而且不知何故,设置舞台图标不会影响对话框图标。

谢谢

【问题讨论】:

    标签: java javafx icons javafx-8


    【解决方案1】:

    Marco Jakob 有一个很棒的教程here,在这里您不仅可以找到如何使用对话框,还可以找到如何解决您的问题。

    对于新的对话框(JDK8u40早期版本或openjfx-dialogs和JDK 8u25),或者ControlsFX中的对话框,为了设置对话框的图标,你可以使用这个solution

    Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
    stage.getIcons().add(
        new Image(this.getClass().getResource("<image>.png").toString()));
    

    这段代码 sn-p 展示了如何使用来自 ControlsFX 的 ProgressDialog,并为对话框设置图标:

    @Override
    public void start(Stage primaryStage) {
    
        Service<Void> service = new Service<Void>() {
            @Override protected Task<Void> createTask() {
                return new Task<Void>() {
                    @Override protected Void call() throws InterruptedException {
                        updateMessage("Message . . .");
                        updateProgress(0, 10);
                        for (int i = 0; i < 10; i++) {
                            Thread.sleep(300);
                            updateProgress(i + 1, 10);
                            updateMessage("Progress " + (i + 1) + " of 10");
                        }
                        updateMessage("End task");
                        return null;
                    }
                };
            }
        };
    
        Button btn = new Button("Start Service");
        btn.setOnAction(e -> {
            ProgressDialog dialog = new ProgressDialog(service);
            dialog.setTitle("Progress Dialog");
            dialog.setHeaderText("Header message");
            Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
            stage.getIcons().add(new Image(this.getClass().getResource("<image>.png").toString()));
            service.start();
        });
    
        Scene scene = new Scene(new StackPane(btn), 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    【讨论】:

    • 另外,我建议在显示对话框之前调用dialog.initOwner(primaryStage)
    • 请注意,此进度对话框会在服务启动时显示,因此无需调用dialog.showAndWait()
    【解决方案2】:

    就这样做吧:

    Alert(AlertType.ERROR, "Erreur de connexion! Verifiez vos Identifiants",FINISH); //Cancel..
    setTitle("XNotes FX Erreur");
    stage = (Stage) alert.getDialogPane().getScene().getWindow();
    stage.getIcons().add(new Image("indiza/XnotesErrorIdz.png")); // To add an icon
    showAndWait();
    

    这是结果

    **朋友们,我们从事的是计算机科学吗? : 不,我们做手工艺品 **

    【讨论】:

    • 应该是 stage.getIcons().add(new Image("indiza/XnotesErrorIdz.png")); // 添加图标
    【解决方案3】:

    这是我在 JavaFX 项目中包含的一种方法,只需调用此方法并将 Alert 作为参数传递即可设置标题栏图标和标题图形。

    public class Msg {
    
    public void showInfo(String title, String header, String message) {
        Alert alertShowInfo = new Alert(Alert.AlertType.INFORMATION);
        addDialogIconTo(alertShowInfo); //add icon and header graphic
        alertShowInfo.setTitle(title);
        alertShowInfo.setHeaderText(header);
        alertShowInfo.setContentText(message);
        alertShowInfo.showAndWait();
    }
    
    //this adds images to Alert
    public void addDialogIconTo(Alert alert) {
        // Add custom Image to Dialog's title bar
        final Image APPLICATION_ICON = new Image("icon.png");
        Stage dialogStage = (Stage) alert.getDialogPane().getScene().getWindow();
        dialogStage.getIcons().add(APPLICATION_ICON);
    
        // Add custom ImageView to Dialog's header pane.        
        final ImageView DIALOG_HEADER_ICON = new ImageView("icon.png");
        DIALOG_HEADER_ICON.setFitHeight(48); // Set size to API recommendation.
        DIALOG_HEADER_ICON.setFitWidth(48);
        alert.getDialogPane().setGraphic(DIALOG_HEADER_ICON);                       
        }
    }
    

    然后,在我希望使用警报的任何类中,它都会有自定义的图标和标题图形。

    public static void main(String[] args){
            Msg msg = new Msg();
    
            // Alert will now include custom icon and header graphic.
            msg.showInfo("Sucess!", "Program succeeded", "Now exiting program");
    } 
    

    【讨论】:

      【解决方案4】:

      通过将应用程序窗口设置为警报框的所有者,您可以轻松地将应用程序的图标用作警报图标:

      @FXML
      Button buShow;
      ...
      
      Alert alert = new Alert(AlertType.INFORMATION, "Nice Box.", ButtonType.CLOSE);
      alert.initOwner(buShow.getScene().getWindow());   // Alert uses the Windows Icon
      alert.show();
      

      【讨论】:

        【解决方案5】:

        与任何对话框类似,但它位于按钮处理程序中。

            Alert alert = new Alert(
                AlertType.WARNING,
                "Alert message here.",
                ButtonType.OK
            );
            alert.initOwner(((Button)event.getSource()).getScene().getWindow());
            alert.setTitle("Alert window title");
            alert.showAndWait();
        
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-05
          • 1970-01-01
          • 2019-01-05
          • 1970-01-01
          • 2016-08-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多