好的,我正在做一个简单的反应游戏,我的代码如下。现在当调用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();
}
}
答案 0 :(得分:4)
您的Thread.sleep()正在GUI线程中运行并使其处于休眠状态,因此无法继续处理其他GUI事件。
Swing的Concurency是你的问题,你会发现这个问题多次回答......
答案 1 :(得分:3)
Event Dispatch Thread上的Swing调用了您的actionPerformed()
方法。在actionPerformed()
方法中,您调用startGame()
,此方法会不断循环。这意味着你永远不会让Event Dispatch Thread继续这样做。