另一种功能方法是使用JOptionPane#showOptionDialog() 方法(如前所述)而不是JoptionPane#showConfirmDialog()。 showOptionDialog() 方法允许您拥有超过您可能需要的大量自定义按钮。
我认为自定义消息区域和按钮文本、颜色和大小的最简单方法是使用基本的 HTML 字符串而不是纯文本字符串。这在所有 JOptionPane 对话框的消息区域中很常见,如果您愿意,您甚至可以通过这种方式将小图像添加到您的文本中。就我而言,它为如何将对话框呈现给最终用户打开了一个全新的灵活世界。如果您真的想要低调而肮脏,请创建您自己的 JDialog 选项窗口对话框,让外观和感觉完全符合您的要求。
对于您当前想要做的事情(使用自定义按钮),JOptionPane#showOptionDialog() 方法就足够了,例如(英文):
这是显示上述确认类型对话框的代码:
// The dialog box title
String title = "Exit Application?"; // HTML won't work here :(
// The dialog's Message area - Basic HTML can be used here. :)
String message = "<html><font size='4'>Are you Sure"
+ " you want to exit this application?</font><br><br><center>"
+ "<font size='5',color=blue><b>My Application Name</b>"
+ "</font></center><br></html>";
/* The desired buttons for the dialog Box. - Basic HTML can be used here. :)
The buttons are automatically sized based on the font size of the text
applied to the button Text (caption). */
String[] optionButtons = {
// The YES Button.
"<html><font size='5',color=red> Yes </font><br>"
+ "<center><font size='1',color=gray>....</font></center</html",
// The NO button.
"<html><font size='5'>No! Not Now</font><br>"
+ "<center><font size='1',color=gray>....</font></center</html",
// The NOT SURE button. :)
"<html><font size='5'>Well, Not Sure ◔̯◔</font><br>"
+ "<center><font size='2'>(I think)</font></center</html"};
// Display the JOptionPane dialog.
/* The Swing Component that will be parent for this Dialog.
Use 'this' if the class this code is run in an extend JFrame.
If the code is run through an action event of a JButton
then just provide the variable name for that button. If
worse comes to worse...just supply: null. */
java.awt.Component comp = (java.awt.Component) null;
int result = JOptionPane.showOptionDialog(comp, message, title,
int result = JOptionPane.showOptionDialog(comp, message, title,
JOptionPane.DEFAULT_OPTION, // Button Options - DEFAULT_OPTION - moot here.
JOptionPane.QUESTION_MESSAGE, // The internal image to use.
null, // No custom icon
optionButtons, // Button Captions (Text)
optionButtons[1] // Default focused button - NO is made to have default focus.
);
switch (result) {
// Yes button selected
case 0:
System.out.println("You selected: Yes! Please");
break;
// No button selected
case 1:
System.out.println("You selected: No! Not now.");
break;
// Don't Know button selected
case 2:
// Just a little decision making between Yes and No.
while (result == 2) {
result = new java.util.Random().nextInt(2);
System.out.println(result);
}
// Display the Decision _ Ternary Operator used here.
System.out.println("You selected: Well, Not Sure - Decided this one for you, " +
(result == 0 ? "Yes selected!" :
result == 1 ? "No Sselected!" :
"Dialog Canceled (Closed)"));
break;
// Dialog simply closed from title bar.
default:
System.out.println("None selected - Dialog Canceled (Closed)");
}
在上面的代码中,JOptionPane 实际上返回对话框中所选按钮的 index 值,并且对话框在该按钮选择时正确关闭(因为它应该是)。
关于上面代码中使用的 HTML 标签:
使用 HTML 字符串时,您需要始终以 <html> 开始字符串,并以结束 </html> 标记结束整个字符串。
<br>
标签。
<b> and </b> 标签。
<u> and </u> 标签。
<font> and
</font>
标签。
<center> and </center> 标签。
&nbsp; - 实体 - 非中断空间。
您可以做很多事情来为 某些 摆动组件(如 JLabels 和 JButtons 等)中的消息框和文本增添趣味(包括工具提示)使用 HTML。