2015-04-01 45 views
4

左側設定圖像I創建爲JavaFX8u40 JavaFX的警告對話框這個非常簡單的例子。上的對話

public class MainApp extends Application 
{ 
    public static void main(String[] args) 
    { 
     Application.launch(args); 
    } 

    private Stage stage; 

    @Override 
    public void start(Stage primaryStage) throws Exception 
    { 
     Button create = new Button("Create Alert"); 
     create.setTooltip(new Tooltip("Create an Alert Dialog")); 
     create.setOnAction(e -> 
     { 
      createAlert(); 
     }); 

     primaryStage.setScene(new Scene(create)); 
     primaryStage.show(); 

     stage = primaryStage; 
    } 

    protected Alert createAlert() 
    { 
     Alert alert = new Alert(AlertType.WARNING); 
     Image image1 = new Image("http://www.mcaprojecttraining.com/images/java-big-icon.png"); 

     ImageView imageView = new ImageView(image1); 

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

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

     return alert; 
    } 

} 

我很感興趣,我可以如何設置對話框左側的圖像。

有人設法改變圖像的一面嗎?

enter image description here

+0

截圖?哪個圖像? – JonasCz 2015-04-01 14:29:28

+0

請發佈其他人可以運行的代碼。沒有人可以訪問您的圖像文件。 – 2015-04-01 14:43:31

+0

@James_D發佈更新 – 2015-04-01 14:58:12

回答

6

如果你看一下頭是如何構建的,你會發現一個GridPane節點佈局左側一個Label併爲圖標StackPane

如果你想通過代碼來扭轉單元順序,你可以這樣做,但它會每次updateHeaderArea()被稱爲覆蓋。

我的建議是使用這個公共API:

dialogPane.setHeader(Node header); 
dialogPane.setGraphic(Node graphic); 

提供header在左側和標籤,和一個空的圖形圖標。

使用相同的方法爲DialogPane,我們可以添加另一個GridPane作爲標題:

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; 
} 

這是你會看到:

Alert