繁体   English   中英

JOptionPane消息对话框上的自定义标题

[英]Custom title on JOptionPane message dialogs

在Java中使用JOptionPane时,如何在消息对话框中创建自定义标题? 我目前有:

JOptionPane.showMessageDialog(null, "You won the game in " + tries + " tries!");

我试图添加另一个参数,这给了我一个错误。 有任何想法吗?

尝试这个

JOptionPane.showMessageDialog(null, "You won the game in 7 tries!", "my title", JOptionPane.INFORMATION_MESSAGE);

您需要提供消息类型(第四个参数),以便秋千知道要显示哪个默认图标。

这是带有标题的对话框的正确语法:

JOptionPane.showMessageDialog(null, "This is the message", "This is the title", JOptionPane.INFORMATION_MESSAGE);
//                          Component; Text to appear in window; Title;            This is the type of dialog (can be error, as well)

注意,有四个参数,而不是三个(@Sanjay解释说,没有一个带有三个参数的方法)

万一想法失败了,我们可以参考JavaDocs ,其中说,

showMessageDialog(Component parentComponent,
                     Object message,
                     String title,
                     int messageType)

顺便说一句,您的想法是正确的:)但请注意,

  1. 没有带有三个参数的方法, 但是它具有四个参数,是的,第三个是对话框的标题 ,为String。
  2. 参数不是用+隔开,而是用,

我这样尝试过:

JOptionPane.showMessageDialog(frame,message,title, JOptionPane.INFORMATION_MESSAGE);

哪里

JFrame frame = new JFrame();
String message = "Population: "
String title = "City Info:"
JOptionPane.INFORMATION_MESSAGE is messageType

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM