Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在等待线程时,它不会显示它应该显示的图标_Java_Multithreading_Swing_Synchronized - Fatal编程技术网

Java 在等待线程时,它不会显示它应该显示的图标

Java 在等待线程时,它不会显示它应该显示的图标,java,multithreading,swing,synchronized,Java,Multithreading,Swing,Synchronized,我正在尝试创建一个记忆游戏。当第一个和第二个按钮都有相同的图像时,我将它们的图像设置为null。此外,为了关联一个值(即图像),我使用JButton创建了classextensingJButtonclass public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub if (e.getButton() == MouseEvent.BUTTON1) { if (game.ti

我正在尝试创建一个记忆游戏。当第一个和第二个按钮都有相同的图像时,我将它们的图像设置为null。此外,为了关联一个值(即图像),我使用JButton创建了class
extensing
JButton
class

public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    if (e.getButton() == MouseEvent.BUTTON1) {
        if (game.timePassed == 0) {
            game.clock.start();
        }
        game.openButton(this);
        game.checkIfWon();
    }
}
这段代码调用另一个类中的
openButton()
方法

public void openButton(UsingJButton b) {
        // TODO Auto-generated method stub

    if (b.isEnabled() && !button1) {
        b.setIcon(new ImageIcon(upperImages[b.value].getImage().getScaledInstance(50, 50,
                java.awt.Image.SCALE_SMOOTH)));
        b1 = new UsingJButton(b.row, b.col, this);
        b1 = b;
        button1 = true;
    } else if (b.isEnabled() && button1 && !button2 && ((b1.row != b.row) || (b1.col != b.col))) {
        synchronized (b) {
            b.setIcon(new ImageIcon(upperImages[b.value].getImage().getScaledInstance(50, 50,
                    java.awt.Image.SCALE_SMOOTH)));
            button2 = true;
            try {
                b.wait(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    if (button1 && button2) {
        if (b1.value == b.value) {
            b1.setVisible(false);
            b.setVisible(false);
            flagCount += 1;
            paint();
            repaint();
        } else {
            b1.setIcon(null);
            b.setIcon(null);
        }
        button1 = button2 = false;
    }

}
我担心的是在这个片段中

synchronized (b) {
    b.setIcon(new ImageIcon(upperImages[b.value].getImage().getScaledInstance(50, 50,
            java.awt.Image.SCALE_SMOOTH)));
    button2 = true;
    try {
        b.wait(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
当我获取锁定按钮
b
时,它会适时等待2秒钟,但按钮上的图标不会显示。我已经在这个类中声明了我的
ImageIcon[]
数组(在定义了
openButton()
的类中),而我的按钮是使用jbuttons的
类。

按钮上的图标应该显示为对游戏很重要的图标。

你在哪里调用
notify
来恢复
wait
状态?请发布一个完整的可测试代码。@Braj我根本没有使用notify。我读过一篇文章,一个线程要等待一个对象,它必须获得它的对象的锁,我已经获得了锁,随后我传递了2秒的参数,在这之后它停止等待,线程可以竞争对象b