有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程Java GUI未更改

好的,我正在做一个简单的反应游戏,我的代码如下。现在,当调用startGame()时,如果我注释掉while循环,一切都会正常进行,但当我使用线程运行它时。睡眠(1000)这东西卡住了!整个程序执行,什么也没发生。请花2分钟运行代码并帮助我调试它。我在用线。睡眠(1000)以查看用户是否可以单击点亮的jPanelArray JPanel。但我相信是因为这根线。睡眠当按下“开始游戏”按钮时,GUI根本不会改变。非常感谢你的帮助

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JExternFrame extends JFrame implements ActionListener{
    public Color randomColor(){
        return new Color((.8f) * generator.nextFloat() + .04f,(.8f) * generator.nextFloat() + .04f,(.8f) * generator.nextFloat() + .04f);
    }
    public JPanel [][]jPanelArray;
    public JButton startButton;
    int []currentLitUp;
    public Random generator;
    public JPanel main;
    public JExternFrame(){
        super("The Reaction Game");
        generator = new Random();
        setSize(400,400);
        startButton = new JButton("Start Game");
        startButton.setOpaque(true);
        startButton.setBorderPainted(false);
        startButton.setBackground(randomColor());
        add(startButton,BorderLayout.NORTH);
        main = new JPanel();
        main.setBackground(randomColor());
        add(main);
        mouseClick myMouseClick = new mouseClick();
        startButton.addActionListener(this);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    public void startGame(){
        startButton.setVisible(false);
        int numOfClicks = generator.nextInt(8)+5;
        int sizeX = 15;
        int sizeY = 15;
        currentLitUp = new int[2];
        jPanelArray = new JPanel[sizeX][sizeY];
        mouseClick mouseClicked = new mouseClick();
        main.setLayout(new GridLayout(sizeX,sizeY));
        for(int i = 0; i < sizeX; i++){
            for(int j = 0; j < sizeY; j++){
                jPanelArray[i][j] = new JPanel();
                jPanelArray[i][j].setBackground(Color.GRAY);
                main.add(jPanelArray[i][j]);
                jPanelArray[i][j].addMouseListener(mouseClicked);
            }
        }
        while (numOfClicks != 0){   
            numOfClicks--;
            currentLitUp[0] = generator.nextInt(14);
            currentLitUp[1] = generator.nextInt(14);
            jPanelArray[currentLitUp[0]][currentLitUp[1]].setBackground(randomColor());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            jPanelArray[currentLitUp[0]][currentLitUp[1]].setBackground(Color.GRAY);
            currentLitUp[0] = -1;
            currentLitUp[1] = -1;
        }
        System.exit(0);
    }
    public class mouseClick implements MouseListener{

        @Override
        public void mouseClicked(MouseEvent e) {
            if(e.getSource() == jPanelArray[currentLitUp[0]][currentLitUp[1]]){
                System.out.print("Correct");
            }
            else System.out.print("INCORRECT");

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == startButton){
            startGame();
        }
    }
    public static void main(String[] args){
         new JExternFrame();
    }
}

共 (2) 个答案

  1. # 2 楼答案

    你的线。sleep()正在GUI线程中运行,并将其置于睡眠状态,因此它无法继续处理其他GUI事件

    Swing中的一致性是你的问题,你会发现这个问题被多次回答