ImageIcon帮助+关闭JOptionPane [英] ImageIcon Help + Close a JOptionPane

查看:75
本文介绍了ImageIcon帮助+关闭JOptionPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您点击右键时,我正试图让PayPal徽标出现。不幸的是,所有显示的都是带有咖啡和一支笔的默认Java徽标。

I'm trying to get the PayPal logo come up when you click the 'rightbutton' is clicked. Unfortunately, all that shows is the default Java logo with the cup of coffee and a pen.

另外,如果点击确定或,我怎么能这样做?取消它关闭了JOptionPane,当前,当你点击确定没有任何反应时,它会一直给你确定/取消选项。

Also, how can I make it so once you click "OK" or "Cancel" it closes the JOptionPane, currently, when you click "OK" nothing happens, it keeps giving you the "OK" / "Cancel" option.

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)


推荐答案

我认为你的参数周围错误

I think you have your parameters around the wrong way

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 声明< a href =http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html#showOptionDialog%28java.awt.Component,%20java.lang.Object,%20java.lang。字符串,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object%5B%5D,%20java.lang.Object%29rel =nofollow noreferrer> showOptionDialog 具有以下参数(按顺序)

JOptionPane declares showOptionDialog as having the following parameters (in order)


  • 组件,与对话框关联的父组件

  • 对象,要显示的消息

  • String ,对话框标题

  • int ,选项类型(如果未指定),例如 JOptionPane.OKAY_CANCEL_OPTION

  • int ,消息类型,例如 JOptionPane.INFORMATION_MESSAGE ,它可以定义对话框将使用的图标

  • 图标,对话框中显示的图标

  • Object [] ,可供用户使用的选项(按钮)

  • Object ,给予焦点的初始选项

  • Component, the parent component associated with the dialog
  • Object, the message to be displayed
  • String, the dialog title
  • int, the type of options (if not specified), such as JOptionPane.OKAY_CANCEL_OPTION
  • int, the message type, such as JOptionPane.INFORMATION_MESSAGE, which can define the icon that the dialog will use
  • Icon, the icon to be displayed on the dialog
  • Object[], the options to be made available to the user (buttons)
  • Object, the initial option which is given focus

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

You seem to be passing...


  • null 作为父母,好的。

  • 恭喜,你点击了按钮。,好的

  • 恭喜,好的

  • JOptionPane.DEFAULT_OPTION ,好的

  • JOptionPane.INFORMATION_MESSAGE 可能没关系,我会使用 JOptionPane.PLAIN_MESSAGE ,但那是我

  • null ...没有图标?

  • new Object [] {panel} ,不确定是否这没关系,但至少它在正确的位置

  • icon ...这甚至不是一个值我可以选择你传递的参数...认为这是错过的地方。

  • null as the parent, okay.
  • "Congratulations, you clicked the button.", okay
  • "Congrats", okay
  • JOptionPane.DEFAULT_OPTION, okay
  • JOptionPane.INFORMATION_MESSAGE probably okay, I'd use JOptionPane.PLAIN_MESSAGE, but that's me
  • null...no icon?
  • new Object[] { panel }, not sure if this is okay, but at least it's in the right place
  • icon...this isn't even a value i the option parameters you passed ... think this is missed place.

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

The problem with the buttons comes down to the fact that they are disconnected from the dialog. The dialog has absolutely no way to know that the buttons are been clicked, you'd have to provide that functionality via some kind of ActionListener...to be honest, it'd just pass String values as the options parameter and let JOptionPane render them as buttons, as it will then deal with closing the dialog, otherwise it gets real messy, real quick...

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

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