我有一个GUI类Gui:

public class Gui extends JFrame implements Runnable
{

private JPanel outer, inner;
private JLabel[][] labels = new JLabel[22][12];
private Color[][] defaultMap, map;
Thread t;
private int row, col;
private Color color;

public Gui()
{
    Container content = getContentPane();
    content.setLayout(new BorderLayout());

    setBackground(Color.BLACK);
    setSize(1000, 1000);
    setLocation(300, 0);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);

    defaultMap = createMap();
    draw(defaultMap);
}

public Color[][] createMap()
{
    Color[][] map = new Color[22][12];
    for (int i = 0; i < 22; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            map[i][j] = Color.WHITE;
        }
    }
    for (int i = 0; i < 22; i++)
    {
        map[i][0] = Color.GRAY;
        map[i][11] = Color.GRAY;
    }
    for (int i = 0; i < 12; i++)
    {
        map[0][i] = Color.GRAY;
        map[21][i] = Color.GRAY;
    }
    return map;
}

public void draw(Color[][] map)
{

    outer = new JPanel();
    outer.setLayout(new BorderLayout());
    outer.setBackground(Color.WHITE);
    outer.setPreferredSize(new Dimension());

    inner = new JPanel();
    inner.setLayout(new GridLayout(22, 12, 2, 2));
    inner.setBackground(Color.BLACK);

    for (int i = 0; i < 22; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            labels[i][j] = new JLabel();
            JLabel label = labels[i][j];
            label.setPreferredSize(new Dimension(20, 20));
            label.setBackground(map[i][j]);
            label.setOpaque(true);
            inner.add(label);
        }
    }

    add(outer);
    add(inner);
    pack();
}

public void move(int row, int col, Color color)
{
    System.out.println(row+","+col);
    map = defaultMap;
    map[row][col] = color;
    t = new Thread(this);
    t.start();
}

@Override
public void run()
{
    draw(map);
}
}

这是从我的主类中调用的,如下所示:
public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                gui = new Gui();
                gui.setVisible(true);
                gui.move(2,5,Color.GREEN);
                Thread.sleep(1000);
                gui.move(3,5,Color.GREEN);
                Thread.sleep(1000);
                gui.move(4,5,Color.GREEN);
            } catch (InterruptedException ex)
            {
                Logger.getLogger(Tetris.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

因此,当调用gui.move()函数时,发生了奇怪的事情。您可以忽略其余部分,也可以使用它(如果有帮助)。但是每次,应该在2,5处将绿色块“添加”到gui。 3,5;和4,5;一秒钟之后。

问题:

Gui暂时保持黑色状态,然后立即使用正确的网格和颜色重新粉刷/更新,并且前两个块正确地着色为绿色,但最后一个块的位置为4,5。同样,最初的“defaultMap”应立即绘制,但并非如此,JFrame是黑色的,直到所有内容一次都绘制在一起减去最后一个绿色块为止。然后,每个绿色块应在另一个之后的1秒钟内绘制。

有趣的是,Gui中move方法中的System.out.println()位会按我期望的那样打印出该行和col ...它们在终端中一秒接一秒地出现。因此,这告诉我事情进展顺利。但我不确定Gui的情况是什么...

编辑:故事上的细微差别。仔细检查后,我注意到,在绘制完整个 map 后,最后一个绿色块确实会出现一秒钟,但立即“消失”了,重新绘制了白色。

最佳答案

您正在EDT(事件调度线程)上 sleep 。您绝不应该阻止EDT。

  • 切勿在EDT上调用sleepwait
  • 将所有长时间运行的任务移至其他线程(使用ExecutorsSwingWorker)
  • 要重复或延迟更新UI,可以使用javax.swing.Timer
  • 所有UI操作都应在EDT上执行(使用SwingUtilities.invokeLaterjavax.swing.TimerSwingWorker

  • swing tag wiki中了解有关Swing并发的更多信息。

    09-16 17:31