简体   繁体   English

如何在确认对话框中添加图像图标?

[英]How to add an image icon to a confirmation dialog box?

The position and size does not matter for now, all I require is to set a custom image icon instead of a default java icon into the dialog box. 现在的位置和大小无关紧要,我只需要在对话框中设置自定义图像图标,而不是默认的Java图标即可。

Here's what I have done so far: 到目前为止,这是我所做的:

public void actionPerformed(ActionEvent a) {
  int x=0;

  // Import Asus logo
  ImageIcon img3 = new ImageIcon("AsusConfirmation.jpeg");

  // Create user-friendly information message
  int c = JOptionPane.showConfirmDialog(null, "Your grand total is $"+x+"!\nIs this order correct?", "Checkout", JOptionPane.YES_NO_OPTION, img3);

  if(c==JOptionPane.YES_OPTION) {
    System.exit(0);
  }
}

The error I get with this code is that I can't convert the Image into an int, and I remove the int part, then I won't be able to use: 我收到此代码的错误是我无法将Image转换为int,并且删除了int部分,然后将无法使用:

if(c==JOptionPane.YES_OPTION) {
  System.exit(0);
}

You are not using the right method signature. 您没有使用正确的方法签名。 See at the Javadoc : 参见Javadoc

public static int showConfirmDialog(Component parentComponent,
                Object message,
                String title,
                int optionType,
           ->   int messageType,  <-
                Icon icon)
                         throws HeadlessException

What you are doing is falling in a different signature that does not give an Icon : 您正在做的事情是陷入另一个不提供Icon签名中:

public static int showConfirmDialog(Component parentComponent,
                Object message,
                String title,
                int optionType,
                int messageType)
                         throws HeadlessException

But fail to give an int , causing the compiler to complain about a type mismatch. 但是无法给出int ,导致编译器抱怨类型不匹配。

What you have to do is, add the missing messageType argument, then you'll match the first method signature. 您要做的是,添加缺少的messageType参数,然后匹配第一个方法签名。

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

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