2016-03-30 92 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; 
}