简体   繁体   English

Java中的ImageIcon

[英]ImageIcon in java

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. 我正在研究匹配照片的游戏,我想我可以通过知道conteiner lable的名称来重置imageIcon,然后在isMatch方法中ismatch return false时重置标签图标。

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);

                }

            }

}

I suggest that you don't pass ImageIcons into your ismatch(...) method but rather pass in the two JLabels that hold the ImageIcons. 我建议您不要将ImageIcons传递到ismatch(...)方法中,而是传递两个保存ImageIcons的JLabel。 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. 然后,在该方法内部,您可以像以前一样提取ImageIcons并将它们进行比较,但更重要的是,您拥有保存图标的JLabel的引用,然后可以将它们设置为背景或空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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM