Java中的ImageIcon [英] ImageIcon in java

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

问题描述

我正在开发匹配照片的游戏,我认为我可以通过知道conteiner lable的名称来重置imageIcon,然后在isMatch方法中ismatch返回false时重置标签图标.

i am working on matching photos game, i think i may be able to reset imageIcon by know the name of conteiner lable then reset the label icon when ismatch return false in the isMatch method.

在每个标签中写入以下代码,仅在第二个标签中才能进行重置..我该怎么办?

the write the following code in each label, the reset work only in the second label .. what should i do ?

public ImageIcon firstChoice;
    public ImageIcon SecoundChoice;
    public boolean isSelected = false;

    public boolean isMatch = true;

    public boolean ismatch(ImageIcon firstChoice, ImageIcon secoundChoce) {

        if (firstChoice.getImage() == secoundChoce.getImage()) {
            JOptionPane.showMessageDialog(null, " wowo you got it ^^");
            isMatch = true;
        } else {
            JOptionPane.showMessageDialog(null, "  notmatced");
            isMatch = false;


        }
        return isMatch;
    }


// label Mouse Clicked

private void label1MouseClicked(java.awt.event.MouseEvent evt) { 

    label1.setIcon(new ImageIcon("G:/Games/icons/File Server Asia.png"));

            if (isSelected == true) {
                ImageIcon icon1 = (ImageIcon) label1.getIcon();
                firstChoice = icon1;
                if (SecoundChoice != null && firstChoice != null) {
                }
                boolean match = ismatch(firstChoice, SecoundChoice);
                if (isMatch == false) {
                    label1.setIcon(null);
                    firstChoice = SecoundChoice = null;

                }

            } else {
                if (SecoundChoice == null) {

                    ImageIcon icon1 = (ImageIcon) label1.getIcon();
                    SecoundChoice = icon1;
                    isSelected = true;

                }


                if (isMatch == false) {
                    label1.setIcon(null);

                }

            }

}

推荐答案

我建议您不要将ImageIcons传递到您的ismatch(...)方法中,而是传递两个保存ImageIcons的JLabel.然后,在该方法内部,您可以像以前一样提取ImageIcons并进行比较,但更重要的是,您可以引用保存图标的JLabel,然后可以将它们设置为背景或空Icon.

I suggest that you don't pass ImageIcons into your ismatch(...) method but rather pass in the two JLabels that hold the ImageIcons. Then inside the method you can extract the ImageIcons and compare them, same as before, but more importantly, you have a reference to the JLabels that hold the icons, and you can then set them to the background or null Icon.

// "second" is mispelled
public boolean ismatch(JLabel firstChoiceLabel, JLabel secoundChoceLabel) {

    ImageIcon firstChoice = firstChoiceLabel.getIcon();
    ImageIcon secoundChoice = secoundChoiceLabel.getIcon(); 

    if (firstChoice.getImage() == secoundChoce.getImage()) {
        JOptionPane.showMessageDialog(null, " wowo you got it ^^");
        isMatch = true;
    } else {
        JOptionPane.showMessageDialog(null, "  notmatced");
        isMatch = false;

        // here set Icon to null or to background icon.
        firstChoiceLabel.setIcon(null);
        secoundChoiceLabel.setIcon(null);
    }
    return isMatch;
}

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

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