如何使用javax.swing.Timer暂停GUI程序

如何使用javax.swing.Timer暂停GUI程序,java,swing,user-interface,timer,event-dispatch-thread,Java,Swing,User Interface,Timer,Event Dispatch Thread,在问这个问题之前,我已经做了研究并尝试了几件事情,但似乎没有任何效果 下面是我的代码的简化版本 我有一个程序,基本上可以在棋盘上移动棋子。该表是使用类JTable实现的。我试图做到的是在每次移动之间创造几秒钟的延迟 表是一个扩展JTable的类。构造器在桌子上创建一个棋子 public Table (int rows, int columns) { ChessPiece piece = new ChessPiece(); } 桌子上有一个菜单,可以选择移动棋子。此选项将通过调用move

在问这个问题之前,我已经做了研究并尝试了几件事情,但似乎没有任何效果

下面是我的代码的简化版本

我有一个程序,基本上可以在棋盘上移动棋子。该表是使用类JTable实现的。我试图做到的是在每次移动之间创造几秒钟的延迟

表是一个扩展JTable的类。构造器在桌子上创建一个棋子

public Table (int rows, int columns)
{
    ChessPiece piece = new ChessPiece();
}
桌子上有一个菜单,可以选择移动棋子。此选项将通过调用moveForward()方法向前移动棋子

要启动程序,请执行以下操作:

Table table = new Table();
javax.swing.SwingUtilities.invokeLater( new Runnable()
                                        { public void run()
                                          {
                                              table.setVisible(true);
                                          }
                                        } );
public void moveForward()
{
    // Sleep for a couple of seconds before moving.
    try { Thread.sleep(SLEEP_TIME);}
    catch(InterruptedException ex) { Thread.currentThread().interrupt(); }

    .....<code to move the chess piece forward>....
}
public void moveForward()
{
    ActionListener taskPerformer = new ActionListener() {
                                       public void actionPerformed(ActionEvent evt)
                                       {
                                           System.out.println("Hello!");

                                       }
                                     };
    javax.swing.Timer t = new javax.swing.Timer(10000, taskPerformer);
    t.setRepeats(false);
    t.start();

    .....<code to move the chess piece forward>....
}
因此,为了创造几秒钟的延迟,我首先尝试了以下方法:

Table table = new Table();
javax.swing.SwingUtilities.invokeLater( new Runnable()
                                        { public void run()
                                          {
                                              table.setVisible(true);
                                          }
                                        } );
public void moveForward()
{
    // Sleep for a couple of seconds before moving.
    try { Thread.sleep(SLEEP_TIME);}
    catch(InterruptedException ex) { Thread.currentThread().interrupt(); }

    .....<code to move the chess piece forward>....
}
public void moveForward()
{
    ActionListener taskPerformer = new ActionListener() {
                                       public void actionPerformed(ActionEvent evt)
                                       {
                                           System.out.println("Hello!");

                                       }
                                     };
    javax.swing.Timer t = new javax.swing.Timer(10000, taskPerformer);
    t.setRepeats(false);
    t.start();

    .....<code to move the chess piece forward>....
}
public void moveForward()
{
//移动前先睡几秒钟。
试试{Thread.sleep(sleep_TIME);}
catch(InterruptedException ex){Thread.currentThread().interrupt();}
…<向前移动棋子的代码>。。。。
}
当我点击“移动棋子”选项时,整个GUI就会冻结。我不清楚为什么程序的行为如此。如果有人能用简单的英语向我解释,我将不胜感激

我环顾四周,有人建议我应该使用java.swing.Timer。因此,我将代码更改为:

Table table = new Table();
javax.swing.SwingUtilities.invokeLater( new Runnable()
                                        { public void run()
                                          {
                                              table.setVisible(true);
                                          }
                                        } );
public void moveForward()
{
    // Sleep for a couple of seconds before moving.
    try { Thread.sleep(SLEEP_TIME);}
    catch(InterruptedException ex) { Thread.currentThread().interrupt(); }

    .....<code to move the chess piece forward>....
}
public void moveForward()
{
    ActionListener taskPerformer = new ActionListener() {
                                       public void actionPerformed(ActionEvent evt)
                                       {
                                           System.out.println("Hello!");

                                       }
                                     };
    javax.swing.Timer t = new javax.swing.Timer(10000, taskPerformer);
    t.setRepeats(false);
    t.start();

    .....<code to move the chess piece forward>....
}
public void moveForward()
{
ActionListener taskPerformer=新建ActionListener(){
已执行的公共无效操作(操作事件evt)
{
System.out.println(“你好!”);
}
};
javax.swing.Timer t=新的javax.swing.Timer(10000,taskPerformer);
t、 设置重复(假);
t、 start();
…<向前移动棋子的代码>。。。。
}
当我点击移动棋子选项时,GUI不再冻结,但它什么也不做。没有我预期的10秒延迟

请告诉我这里出了什么问题


提前感谢。

将棋子从起始位置一个接一个地移动到棋盘中央的前面,这似乎非常有效


为了更快地获得更好的帮助,请发布一个行为不同的代码(如下面的代码)。此MCTRE基于中看到的代码

import java.awt.*;
导入java.awt.event.*;
导入java.awt.image.buffereImage;
导入javax.swing.*;
导入javax.swing.border.*;
导入java.net.URL;
导入javax.imageio.imageio;
公共类动画chessgui{
私有最终JPanel gui=新JPanel(新边界布局(3,3));
私有JButton[][]棋盘方块=新JButton[8][8];
私有图像[][]棋子图像=新图像[2][6];
私人JPanel棋盘;
专用最终JLabel消息=新JLabel(
“国际象棋冠军已经准备好了!”;
私有静态最终字符串COLS=“ABCDEFGH”;
公共静态最终整数皇后=0,国王=1,
车=2,骑士=3,主教=4,卒=5;
公共静态最终int[]起始行={
车,骑士,主教,国王,王后,主教,骑士,车
};
公共静态最终整数黑色=0,白色=1;
私人定时器;
动画棋盘格(){
初始化gui();
}
公共无效动画片段(){
计时器=新计时器(1000,新ActionListener(){
整数计数=0;
@凌驾
已执行的公共无效操作(操作事件e){
如果(计数%2==0){
棋盘格[count/2][1]。设置图标(空);
棋盘格[count/2][3]。设置图标(新图像图标(
棋子[黑色][棋子]);
}否则{
棋盘格[count/2][6]。设置图标(空);
棋盘格[count/2][4]。设置图标(新图像图标(
棋子[白色][棋子]);
}
计数++;
如果(计数=16){
timer.stop();
}
}
});
timer.start();
}
公共最终无效初始化gui(){
//为棋子创建图像
createImages();
//设置主GUI
setboorder(新的EmptyBorder(5,5,5,5));
JToolBar工具=新的JToolBar();
工具。可设置浮动(错误);
添加(工具、边框布局、页面开始);
Action newGameAction=新抽象动作(“新”){
@凌驾
已执行的公共无效操作(操作事件e){
setupNewGame();
}
};
添加(newGameAction);
添加(新的JButton(“Save”);//TODO-添加功能!
添加(新的JButton(“还原”);//TODO-添加功能!
tools.addSeparator();
tools.add(新的JButton(“辞职”);//TODO-添加功能!
tools.addSeparator();
工具。添加(消息);
添加(新JLabel(“?”),BorderLayout.LINE_START);
棋盘=新的JPanel(新的网格布局(0,9)){
/**
*重写首选大小以返回最大值,以
*正方形。必须(必须,必须)添加到GridBagLayout中
*唯一的组件(它使用父级作为大小指南)具有
*无网格约束(因此它居中)。
*/
@凌驾
公共最终维度getPreferredSize(){
维度d=super.getPreferredSize();
维度prefSize=null;
组件c=getParent();
如果(c==null){
prefSize=新维度(
(int)d.getWidth(),(int)d.getHeight();
}如果(c!=null,则为else
&&c.getWidth()>d.getWidth()
&&c.getHeight()