通过 setName() 比较组件.

2022-01-25 00:00:00 compare java swing

我正在编写一个图像益智游戏,其中一部分代码是将用户选择的部分与正确图像的部分进行比较.

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.

当用户将拼图块从原来的 3x3 网格拖动到另一个 3x 网格进行匹配后,比较就开始了.

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.");
                    }
            }
        }

推荐答案

在您的 mouseReleased 事件中,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() 返回触发事件的 Component 对象(您的 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.

相关文章