ImageIcon Help + Close a JOptionPane

前端 未结 2 1114

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

相关标签:
2条回答
  • 2020-12-12 08:03

    I think you have your parameters around the wrong way

    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 declares showOptionDialog as having the following parameters (in order)

    • 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 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.

    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...

    0 讨论(0)
  • 2020-12-12 08:24

    For the ImageIcon, it's because you have 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
    );
    

    So right now you are providing null for the icon and the icon for the initial value. It should be:

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

    For the other question, it looks like you are providing your own options as some kind of panel. I guess technically what you could do is use a constructor so you have a reference to the JOptionPane that you can dismiss programatically but you do not need to do this. Instead, I would recommend using the regular OK_CANCEL_OPTION which will do this for you.

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

    Edit for your comment:

    • Try replacing the backslashes in the path with forward slashes. The doc says the path is converted to URL and to use forward slashes. See also File.separator vs Slash in Paths.
    • The doc says the image is loaded in the background. So in other words you might be displaying the image before the data is loaded. There's two things you could do. First, preloading the image at the start of the program by making it a static member, second using ImageIO which will load the image fully immediately.

    You should really preload the image anyway. Otherwise you are reading the file every time you show the dialog.

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题