更改JOptionPane中的图标 [英] Changing the icon in JOptionPane

查看:299
本文介绍了更改JOptionPane中的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展 JOptionPane 的类。在其中有一个方法调用 showConfirmDialog(new JFrame(),(JScrollPane)jp,Friends List,2,0,icon);

I have a class that extends JOptionPane. In it there's a method that calls showConfirmDialog (new JFrame(), (JScrollPane) jp, "Friends List", 2, 0, icon);

有没有办法更改图标而无需再次调用 showConfirmDialog ?也就是说,根据我在 JOptionPane 中的输入,我可以在不进行新确认对话的情况下更改图标吗?

Is there a way to change the icon without having to call showConfirmDialog a second time? That is, based on my input in the JOptionPane, can I change the icon without making a new confirm dialog?

推荐答案

如图所示此处,您可以将 JOptionPane 添加到对话框并收听所需的的PropertyChangeEvent 。以下示例在响应单击按钮时在两个 UIManager 图标之间切换。

As shown here, you can add a JOptionPane to a Dialog and listen for the desired PropertyChangeEvent. The example below switches between two UIManager icons in response to clicking the buttons.

JDialog d = new JDialog();
d.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final Icon PENDING = UIManager.getIcon("html.pendingImage");
final Icon MISSING = UIManager.getIcon("html.missingImage");
final JOptionPane optionPane = new JOptionPane("Click a Button",
    JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
optionPane.addPropertyChangeListener(new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent e) {
        if (e.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) {
            Integer value = (Integer) e.getNewValue();
            if (value.intValue() == JOptionPane.YES_OPTION) {
                optionPane.setIcon(PENDING);
            } else {
                optionPane.setIcon(MISSING);
            }
        }
    }
});
d.setContentPane(optionPane);
d.pack();
d.setLocationRelativeTo(null);
d.setVisible(true);

这篇关于更改JOptionPane中的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆