2017-02-12 66 views
1

所以,我想弄清楚如何在我的纸牌游戏中添加暂停以使CPU像轮流转动一样。但是,涉及的代码太多,所以我认为如果我们在这个代码上演示策略,它可以工作。如何让这个项目在按下按钮后暂停一段时间

如何让此代码暂停,例如按下按钮5秒后,然后打印消息。

public class TimerTest extends JFrame implements ActionListener{ 

private static final long serialVersionUID = 7416567620110237028L; 
JTextArea area; 
Timer timer; 
int count; // Counts the number of sendings done by the timer 
boolean running; // Indicates if the timer is started (true) or stopped (false) 

public TimerTest() { 
    super("Test"); 
    setBounds(30,30,500,500); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setLayout(null); 

    area = new JTextArea(); 
    area.setBounds(0, 0, 500, 400); 
    add(area); 

    JButton button = new JButton("Click Me!"); 
    button.addActionListener(this); 
    button.setBounds(200, 400, 100, 40); 
    add(button); 

    // Initialization of the timer. 1 second delay and this class as ActionListener 
    timer = new Timer(5000, this); 
    timer.setRepeats(true); // Send events until someone stops it 
    count = 0; // in the beginning, 0 events sended by timer 
    running = false; 
    System.out.println(timer.isRepeats()); 
    setVisible(true); // Shows the frame 
} 

public void actionPerformed(ActionEvent e) { 
    if (! running) { 
     timer.start(); 
     running = true; 
    } 
    // Writing the current time and increasing the cont times 
    area.append(Calendar.getInstance().getTime().toString()+"\n"); 
    count++; 
    if (count == 10) { 
     timer.stop(); 
     count = 0; 
     running = false; 
    } 
} 

public static void main(String[] args) { 
    // Executing the frame with its Timer 
    new TimerTest(); 
} 
} 

我真正想要做的是能够在运行每个CPU的动作的方法中插入一个暂停。但是,听起来像是用Timer类,你需要在Timer的动作监听器中执行你的post-pause动作。我最关心的是为什么Timer需要一个actionListener。完成后,我不需要定时器重复。

如果我发布自己的代码会更好,请告诉我。但我不确定哪些部分是有用的,因为我不想在此线程中拥有大量的代码。

+0

尝试增加'的Thread.sleep(5000);'? –

+2

Swing'Timer'有什么问题?如果将其设置为不重复,延迟时间为5000,则可以调用另一种方法在指定的时间段后执行所需的任何操作? – MadProgrammer

+0

@DarshanMehta您可能并不知道在Swing或JavaFX中使用Thread.sleep或与任何UI框架相关的危险和问题,这不是一个好主意,并且对于所有额外的麻烦,您不应该推荐它对于这个用例 – MadProgrammer

回答

0

您应该推出一个新的线程,其执行以下操作:

  • 睡眠,持续5秒
  • 设置您需要的信息
  • 呼叫repaint()信号需要重新绘制的消息。
+1

Swing不是线程安全的,OP使用适当的Swing'Timer ',但只是没有正确使用 – MadProgrammer

0

在执行动作的情况下,您可以启动某些功能,如: TimeUnit.SECONDS.sleep(1); 要做到这一点。我在你的代码中看到你正在手动计算时间。你可以让编译器或cpu为你做这个。

+0

Swing不是线程安全的,OP使用相应的Swing'Timer',但只是没有正确使用它 – MadProgrammer

1

从概念上讲,你想在5秒后触发一些事件,Swing Timer是完美的,它是线程安全的,使它更新UI的安全性,而无需更多的箍跳。

Timer设置它自己的线程等待,所以它不会阻塞EDT,这意味着你需要等到定时器被触发后才能做到,它不会阻止并等待你的地方叫它,这是它有一个ActionListener的原因,所以你知道它何时被触发。

您可以使用现有的Timer,假设它没有做别的,而是为了论证起见,我创建一个新的...

private Timer waitItOut; 
private JButton button; 

然后在你的构造函数,你设置的计时器,这里的区别是我将它不重复的,这意味着它不会触发每5秒,但是当你想,你可以重新运行它...

waitItOut = new Timer(5000, new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent evt) { 
     doImportantWork(); 
    } 
}); 
waitItOut.setRepeats(false); 

// I made this an instance field for demonstration purposes 
button = new JButton("Click Me!"); 
button.addActionListener(this); 
button.setBounds(200, 400, 100, 40); 
add(button); 
actionPerformed方法

然后,你只需启动计时器...

public void actionPerformed(ActionEvent e) { 
    button.setEnabled(false); 
    waitItOut.restart(); 
} 

,等到它调用你的 “这应该在指定的延迟后运行的重要方法” ......

public void doImportantWork() { 
    button.setEnabled(false); 
    // Writing the current time and increasing the cont times 
    area.append(Calendar.getInstance().getTime().toString()+"\n"); 
    count++; 
    if (count == 10) { 
     count = 0; 
     running = false; 
    } 
} 
+0

好吧,这与上面的测试程序一起工作!我需要花一段时间才能将其应用到我的纸牌游戏中,但是当我弄明白时,我会报告结果。 – Austen

+0

@Austen部分解决方案是,您需要改变您的思维方式,使用GUI,您不能以线性方式思考,它们是事件驱动的,发生了一些事情并且您对此做出响应 – MadProgrammer

+0

因此,它在我的游戏中起作用。但是,事实证明,我需要让计时器重复。我设置CPU的方式是for循环遍历剩余的玩家,直到他们回到用户。我试图让计时器在他们抽出一张新卡并在他们进行游戏之前暂停CPU的动作。但出于某种原因,它不会在for循环的下一个实例中重新启动计时器。所以它会通过玩家2,然后停在玩家3.我正在尝试使用Eclipse进行调试,它实际上可能只是我的代码,但我不确定 – Austen

相关问题