ImageIcon帮助+关闭JOptionPane

时间:2014-03-05 00:21:57

标签: java image swing joptionpane imageicon

当您点击“右键”时,我正试图让PayPal徽标出现。不幸的是,所有这些都是默认的Java徽标,上面有一杯咖啡和一支笔。

另外,如何点击“确定”或“取消”关闭JOptionPane,当前,当您点击“确定”没有任何反应时,它会一直给您“确定”/“取消”选项

rightbutton = new JButton("Right.");
add(rightbutton);
rightbutton.addActionListener(
        new ActionListener(){
            public void actionPerformed(ActionEvent event){
                //what do we want to happen when we
                //click the button
                final ImageIcon icon = new ImageIcon("C:\\Users\\Scr3am\\Desktop\\paypal.jpg");
                JOptionPane.showOptionDialog(null, "Congratulations, you clicked the button.", "Congrats", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[] { panel }, icon);
            }
        }
);

完整代码

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Password extends JFrame {

    JButton leftbutton;
    JButton centerbutton;
    JButton rightbutton;
    FlowLayout layout;
    Container container;




    Password(){
        super("Toolbar");
        layout = new FlowLayout();
        //get bulk of window, so it knows where to put the stuff
        container = getContentPane();
        setLayout(layout);

        //left stuff in here
        leftbutton = new JButton("Left");
        add(leftbutton);
        leftbutton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        //what do we want to happen when we
                        //click the button
                        layout.setAlignment(FlowLayout.LEFT);

                    }


                }

        );

        final JPanel panel = new JPanel();
        panel.add(new JButton("OK"));
        panel.add(new JButton("Cancel"));

        //center stuff in here
        centerbutton = new JButton("Center");
        add(centerbutton);
        centerbutton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        //what do we want to happen when we
                        //click the button
                        layout.setAlignment(FlowLayout.CENTER);
                        layout.layoutContainer(container);

                    }
                }
        );

        //right stuff in here
        rightbutton = new JButton("Right.");
        add(rightbutton);
        rightbutton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        //what do we want to happen when we
                        //click the button
                        try {
                            final ImageIcon icon = new ImageIcon(ImageIO.read(new File("paypalicon.gif")));

                            JOptionPane.showOptionDialog(
                                    null,
                                    "Congratulations, you clicked the button.",
                                    "Congrats",
                                    JOptionPane.OK_OPTION,
                                    JOptionPane.PLAIN_MESSAGE,
                                    icon,
                                    new Object[]{"Okay"},
                                    "Okay");
                        } catch (IOException exp) {
                            exp.printStackTrace();
                        }



                    }
                }
        );
    }


}

错误:

javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1275)
at Password$3.actionPerformed(Password.java:79)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6414)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6179)
at java.awt.Container.processEvent(Container.java:2084)
at java.awt.Component.dispatchEventImpl(Component.java:4776)
at java.awt.Container.dispatchEventImpl(Container.java:2142)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
at java.awt.Container.dispatchEventImpl(Container.java:2128)
at java.awt.Window.dispatchEventImpl(Window.java:2492)
at java.awt.Component.dispatchEvent(Component.java:4604)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:690)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

2 个答案:

答案 0 :(得分:3)

我认为你的参数是错误的

enter image description here

try {
    final ImageIcon icon = new ImageIcon(ImageIO.read(new File("paypalicon.gif")));

    JOptionPane.showOptionDialog(
            null,
            "Congratulations, you clicked the button.",
            "Congrats",
            JOptionPane.OK_OPTION,
            JOptionPane.PLAIN_MESSAGE,
            icon,
            new Object[]{"Okay"},
            "Okay");
} catch (IOException exp) {
    exp.printStackTrace();
}

JOptionPane声明showOptionDialog具有以下参数(按顺序)

  • Component,与对话框相关联的父组件
  • Object,要显示的消息
  • String,对话标题
  • int,选项类型(如果未指定),例如JOptionPane.OKAY_CANCEL_OPTION
  • int,消息类型,例如JOptionPane.INFORMATION_MESSAGE,可以定义对话框将使用的图标
  • Icon,即对话框中显示的图标
  • Object[],可供用户使用的选项(按钮)
  • Object,重点关注的初始选项

你似乎在路过......

  • null作为父母,好的。
  • "Congratulations, you clicked the button.",好的
  • "Congrats",好的
  • JOptionPane.DEFAULT_OPTION,好的
  • JOptionPane.INFORMATION_MESSAGE可能没关系,我会使用JOptionPane.PLAIN_MESSAGE,但那就是我
  • null ...没有图标?
  • new Object[] { panel },不确定这是否合适,但至少它在正确的位置
  • icon ...这甚至不是我传递的选项参数的值...认为这是错过的地方。

按钮的问题归结为它们与对话框断开连接。该对话框完全没有办法知道按钮被点击了,您必须通过某种ActionListener提供该功能...说实话,它只是传递String值作为options参数并让JOptionPane将它们渲染为按钮,因为它将处理关闭对话框,否则它会变得非常混乱,真正快速......

答案 1 :(得分:3)

对于ImageIcon,这是因为你有the arguments in the wrong order

JOptionPane.showOptionDialog(
    null,
    "Congratulations, you clicked the button.",
    "Congrats",
    JOptionPane.DEFAULT_OPTION,
    JOptionPane.INFORMATION_MESSAGE,
    null,
    new Object[] { panel },
    icon
);

所以现在你为图标和初始值的图标提供null。它应该是:

JOptionPane.showOptionDialog(
    null,
    "Congratulations, you clicked the button.",
    "Congrats",
    JOptionPane.DEFAULT_OPTION,
    JOptionPane.INFORMATION_MESSAGE,
    icon,
    new Object[] { panel },
    null
);

对于另一个问题,看起来您提供自己的选项作为某种面板。我想从技术上讲你可以做的是使用一个构造函数,所以你有一个JOptionPane的引用,你可以以编程方式解除它,但你不需要这样做。相反,我建议使用常规的OK_CANCEL_OPTION,它会为你做这件事。

int optionChosen = JOptionPane.showConfirmDialog(
    null,
    "Congratulations, you clicked the button.",
    "Congrats",
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.INFORMATION_MESSAGE,
    icon
);

编辑您的评论:

  • 尝试使用正斜杠替换路径中的反斜杠。 The doc表示路径已转换为URL并使用正斜杠。另请参阅File.separator vs Slash in Paths
  • 文档说图像是在后台加载的。换句话说,您可能在加载数据之前显示图像。你可以做两件事。首先,通过使图像成为静态成员在程序开始时预加载图像,然后使用ImageIO立即加载图像。

无论如何,你应该真正预装图像。否则,每次显示对话框时都在读取文件。

static final ImageIcon paypalIcon;
static {
    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("your/path"));
    } catch(IOException ioe) {
        ioe.printStackTrace(System.err);
    }

    // set to null if the image failed to load
    // if you pass null to showXXXDialog it uses the Look & Feel default
    // this way the program still works if it can't find the file
    if(img == null) {
        paypalIcon = null;
    } else {
        paypalIcon = new ImageIcon(img);
    }
}