通过setName()比较组件。 [英] Comparing components via setName() .

查看:94
本文介绍了通过setName()比较组件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码一个图像拼图游戏,代码的一部分是将用户选择的片段与正确的图片进行比较。

I am coding an image puzzle game and one part of the code is to compare the pieces the user has selected to the pieces of the correct image.

每个图片作为ImageIcon已添加到JButton。

Each image piece is already added to a JButton as an ImageIcon.

需要使用标识符来区分每个图像,并进行比较。

An identifier is required to differentiate each image piece apart and also for comparision.

我为每个创建为标识符的JButton设置一个setName()。

I am setting a setName() for each JButton created as the identifier.

比较开始时,用户释放鼠标,

The comparison starts when the user releases the mouse after he drags the puzzle pieces from the original 3x3 grid where the shuffled pieces are to the other 3x grid for matching.

我有问题从比较中删除错误 if

I have problems removing the error from the comparison if statement.

我从这个SO线程得到了比较的想法 - 链接

I got the comparison idea from this SO thread - link

    private JButton[] button = new JButton[9];
    private JButton[] waa = new JButton[9];

    private String id;
    private int cc;
    private String id2;
    private int cc2;

    // setName for each of the 9 buttons in the original 3x3 grid being created 
    // which stores the shuffled puzzle pieces
    for(int a=0; a<9; a++){
        button[a] = new JButton(new ImageIcon());
        id += Integer.toString(++cc);
        button[a].setName(id); 
    }

    // setName for each of the 9 buttons in the other 3x3 grid  
    // where the images will be dragged to by the user
        for(int b=0; b<9; b++){
        waa[b] = new JButton();
        id2 += Integer.toString(++cc2);
        waa[b].setName(id2); 
    }

    // check if puzzle pieces are matched in the correct place
    // compare name of original 'button' array button with the name of 'waa' array buttons 
        button[a].addMouseListener(new MouseAdapter(){

            public void mouseReleased(MouseEvent m){
                if(m.getbutton().getName().equals (waa.getName())){

                    }
                    else{
                         JOptionPane.showMessageDialog(null,"Wrong! Try Again.");
                    }
            }
        }


推荐答案

$ c> mouseReleased event m.getButton()返回被点击的鼠标按钮。会使你更近:

In your mouseReleased event m.getButton() is returning the mouse button that was clicked. You'll want to do something more like this that will get you closer:

if (m.getComponent().getName().equals(waa.getName())) {

m.getComponent() 组件对象(您的 JButton )引发事件。从那里你可以与你使用的 getName 方法进行比较。

m.getComponent() returns the Component object (your JButton) that the event was fired from. From there you can do the comparison with the getName approach you are using.

还有一个问题, waa 变量是一个数组。我不知道你想要比较他们,无论是通过数组,并确保索引和名称匹配,但这是一个额外的问题,你需要研究。

There's an additional issue in that your waa variable is an array. I'm not sure how you want to compare them, whether running through the arrays and making sure the index and names match, but that's an additional issue you need to look into.

这篇关于通过setName()比较组件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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