java 在 JOptionPane.showInputDialog 标题中使用自定义图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3925616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:59:17  来源:igfitidea点击:

Using custom icons in title with JOptionPane.showInputDialog

javaswingjoptionpane

提问by Global Dictator

I'm trying to use a custome icon in the title of the JOption pane rather then with the label. Is there a way that I can do that? I'm using the Icon class (as shown below) in JOptionPanebut it keeps displaying the icon in the main display area rather than in the title. Here is the code:

我试图在 JOption 窗格的标题中使用客户图标而不是标签。有没有办法做到这一点?我正在使用 Icon 类(如下所示),JOptionPane但它一直在主显示区域而不是标题中显示图标。这是代码:

Icon icon = new ImageIcon(ApplicationManager.getApplicationImage().getImage());
String jid = (String)JOptionPane.showInputDialog(ApplicationManager.getMainWindow(), 
                 Res.getString("label.enter.address"), 
                 Res.getString("title.start.chat"), 
                 JOptionPane.QUESTION_MESSAGE, 
                 icon,                    
                 null, 
                 selectedUser);

Thanks

谢谢

回答by Andrew Thompson

Try this code..

试试这个代码..

import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.net.URL;

class OptionPaneIcon {

    public static void main(String[] args) throws Exception {
        JOptionPane jop = new JOptionPane(
            "Message",
            JOptionPane.QUESTION_MESSAGE,
            JOptionPane.DEFAULT_OPTION
            );

        JDialog dialog = jop.createDialog("Dialog Title");

        Image image = ImageIO.read(new URL(
            "http://www.gravatar.com/avatar/f1d58f7932b6ae8027c4e1d84f440ffe?s=128&d=identicon&r=PG"));
        dialog.setIconImage( image );
        dialog.setVisible(true);
    }
}

回答by Qwerky

Haven't tried it, but you might get it to work with an internal frame, rather than using a dialog box. Try creating an instance of JOptionPaneand call getInternalFrame(). JInternalFrame has a setFrameIcon(Icon icon)method.

尚未尝试过,但您可能会使其与内部框架一起工作,而不是使用对话框。尝试创建一个实例JOptionPane并调用getInternalFrame(). JInternalFrame 有一个setFrameIcon(Icon icon)方法。

Edit: On course the JInteralFrame's parent must be a JDesktopPane, but apart from this it should work.

编辑:当然,它JInteralFrame的父级必须是 a JDesktopPane,但除此之外它应该可以工作。

回答by wolfcastle

The JOptionPane takes its icon from the parent frame. So you can set the icon on a dummy JFrame, and pass that into the JOptionPane call:

JOptionPane 从父框架中获取其图标。因此,您可以在虚拟 JFrame 上设置图标,并将其传递到 JOptionPane 调用中:

    BufferedImage image = ImageIO.read(new URL(
    "http://www.gravatar.com/avatar/f1d58f7932b6ae8027c4e1d84f440ffe?s=128&d=identicon&r=PG"));
    JFrame frame = new JFrame();
    frame.setIconImage(image);
    JOptionPane.showInputDialog(frame, "Enter Address", "Chat",
            JOptionPane.QUESTION_MESSAGE, null, null, "");

Note, this will likely cause issues with the location of the shown dialog, as it will be placed relative to the dummy JFrame passed in.

请注意,这可能会导致显示对话框的位置出现问题,因为它将相对于传入的虚拟 JFrame 放置。

回答by Global Dictator

HI,

你好,

This would not work as I have some pre selected value that I need to populate the Input dialog box with and hence cant use the JOptionPane constructor but instead have to use showInputDialog method..

这不起作用,因为我有一些预选的值,我需要用它来填充输入对话框,因此不能使用 JOptionPane 构造函数,而必须使用 showInputDialog 方法..

Hence, I believe I cannot use a custome icon when using showInputDialog(.,.,.,.,.,.,.)

因此,我相信在使用 showInputDialog(.,.,.,.,.,.,.) 时我不能使用客户图标

Thanks.

谢谢。