Java 为什么';t我的run()方法无法从Actionlistener正确调用

Java 为什么';t我的run()方法无法从Actionlistener正确调用,java,eclipse,multithreading,Java,Eclipse,Multithreading,我正在开发一个Snake实现,大部分工作已经完成。我唯一的问题是,当我启动新游戏的JMenuItem被单击时,游戏冻结。以下是相关代码: this.newGame.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e){

我正在开发一个Snake实现,大部分工作已经完成。我唯一的问题是,当我启动新游戏的
JMenuItem
被单击时,游戏冻结。以下是相关代码:

             this.newGame.addActionListener(new ActionListener()
                 {
             @Override
             public void actionPerformed(ActionEvent e){
                 neuesSpiel();
             }

                 });





             public void neuesSpiel()
             {     //The old snake game is finished and the playing field is reseted
                  if(snake!=null)
                  {
                      snake.beendeSpiel();

                      for(int i=0; i<20;i++)
                      {
                           for(int j=0;j<10;j++)
                           {    
                            spielfeld[i][j] = false;
                            }
                      }

                  }//the new snake game is created below
                  snake = new Snake(null,null, 10,5, this);     
                  Snake snake2 = new Snake(snake,null,11,5,this);
                  snake.hintermann = snake2;
                  snake2.hintermann = new Snake(snake2,null,12,5,this);

                  snake.run();
               }
我很确定,
run()
方法就是问题所在,因为如果我不调用该方法,游戏就不会冻结。此外,方法
neuesSpiel()
也应该可以,因为当我在构造函数中调用它时,它会按预期工作。 除了使用线程外,我不知道如何让我的函数等待120毫秒。还有别的选择吗? 提前谢谢

除了使用线程外,我不知道如何让我的函数等待120毫秒

您没有使用线程。您正在当前线程中执行
run
方法

如果希望在新线程中运行它,则需要以下内容:

new Thread(snake).start();

好的,谢谢你的信息。但是,如果我运行一个新线程,游戏就会冻结。你知道为什么吗?@Monocle出于同样的原因——你正在阻止Swing事件线程,很可能是在你没有向我们展示的代码中。解决方案:不要这样做,不要阻止EDT。谷歌搜索并阅读“并发和Swing”。学习里面的一切。
new Thread(snake).start();