2012-01-09 46 views
1

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

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

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

回答

12

试试这个

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

您需要提供的信息类型(第4 PARAM),所以摆动知道要显示的默认图标。

4

万一失败的想法,我们有JavaDocs指,它说,

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

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

  1. 没有方法有三个参数, 但它有四个参数,第三个是标题作为字符串的对话框。
  2. 的参数没有被+分开,但通过,
4

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

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解释的那样)

0

我试过这样:

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

其中

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