2016-03-30 38 views
4

我有這樣的代碼片段沒有按鈕:如何改變是文本/ JavaFX的8警報對話框

Alert alert = 
     new Alert(AlertType.WARNING, 
      "The format for dates is year.month.day. " + 
      "For example, today is " + todayToString() + ".", 
      ButtonType.OK, 
      ButtonType.CANCEL); 
alert.setTitle("Date format warning"); 
Optional<ButtonType> result = alert.showAndWait(); 

if (result.get() == ButtonType.OK) { 
    formatGotIt = true; 
} 

上面,我要求兩個按鈕標題爲「是」和「否」。但是,我希望對它們進行更改。

回答

13

您可以定義自己的按鈕類型。在此示例中,按鈕的文本是foobar

ButtonType foo = new ButtonType("foo", ButtonBar.ButtonData.OK_DONE); 
ButtonType bar = new ButtonType("bar", ButtonBar.ButtonData.CANCEL_CLOSE); 
Alert alert = new Alert(AlertType.WARNING, 
     "The format for dates is year.month.day. " 
     + "For example, today is " + todayToString() + ".", 
     foo, 
     bar); 

alert.setTitle("Date format warning"); 
Optional<ButtonType> result = alert.showAndWait(); 

if (result.isPresent() && result.get() == foo) { 
    formatGotIt = true; 
}