如何将图像图标添加到确认对话框?

时间:2016-05-23 21:52:24

标签: java swing joptionpane

现在,位置和大小无关紧要,我只需要在对话框中设置自定义图像图标而不是默认的java图标。

这是我到目前为止所做的事情:

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);
  }
}

我使用此代码得到的错误是我无法将Image转换为int,我删除了int部分,然后我无法使用:

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

1 个答案:

答案 0 :(得分:1)

您没有使用正确的方法签名。请参阅Javadoc

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

您正在做的是使用不同于Icon的其他签名:

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

但是没有给出int,导致编译器抱怨类型不匹配。

您需要做的是,添加缺少的messageType参数,然后您将匹配第一个方法签名。