1

好的,所以我正在制作一个简单的反应游戏,我的代码如下。现在,当调用 startGame() 时,如果我注释掉 while 循环,一切都会好起来的,但是当我用 thread.sleep(1000) 运行它时,事情就卡住了!整个程序执行,没有任何反应。请花 2 分钟运行代码并帮助我调试它。我正在使用 thread.sleep(1000) 来查看用户是否可以单击点亮的 jPanelArray JPanel。但我相信由于 thread.sleep,当按下开始游戏按钮时,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();
    }
}
4

2 回答 2

4

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

Swing 中的并发性是您的问题,您会发现这个问题被多次回答...

于 2012-04-27T06:13:01.637 回答
3

actionPerformed()Swing 在Event Dispatch Thread上调用您的方法。在您的actionPerformed()方法中,您调用startGame()并且此方法不断循环。这意味着您永远不会让 Event Dispatch Thread 继续工作。

您需要调查SwingWorkerSwingUtilities.invokeLater()

于 2012-04-27T06:15:51.563 回答