使用定时器暂停程序执行 [英] Using Timer To Pause Program Execution

查看:33
本文介绍了使用定时器暂停程序执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在指定的时间内暂停 Swing 程序的执行.当然,我使用的第一件事是 Thread.sleep(100) (因为我是菜鸟).然后我知道我的程序不是线程安全的,所以我决定使用 Timer 并听取其他程序员的一些建议.问题是我无法从我可以学习如何使用 Timer 延迟线程的地方获得任何来源.他们中的大多数使用 Timer 来延迟执行.请帮我解决这个问题.我在下面提供了一个可编译的代码片段.

I want to pause the execution of a Swing Program for a specified amount of time. Naturally the first thing that I used was Thread.sleep(100) (since, I am a noob). Then I got to know that my program is not thread safe so I decided to use Timer with some suggestions from fellow programmers. The problem is I am unable to get any sources from where I can learn how to delay the thread, using Timer. Most of them use Timer for delaying execution. Please help me solve this problem. I have provided a compileable code snippet below.

import javax.swing.*;
import java.awt.*;

public class MatrixBoard_swing extends JFrame{

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            MatrixBoard_swing b = new MatrixBoard_swing();      
          }
       });
    }

    MatrixBoard_swing(){
        this.setSize(640, 480);
        this.setVisible(true);
        while(rad < 200){
            repaint();
            rad++;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    int rad = 10;

    public void paint(Graphics g){
        super.paint(g);
        g.drawOval(400-rad, 400-rad, rad, rad); 
    }

}

我对 Timer 实现的试用(如果有错请告诉我):

My trial for a Timer implementation(please tell me if it is wrong):

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MatrixBoard_swing extends JFrame implements ActionListener{

    Timer timer;

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            MatrixBoard_swing b = new MatrixBoard_swing();      
          }
       });
    }

    MatrixBoard_swing(){
        this.setSize(640, 480);
        this.setVisible(true);
        timer = new Timer(100, this);
        timer.start();
    }

    int rad = 10;

    public void paint(Graphics g){
        super.paint(g);
        g.drawOval(400-rad, 400-rad, rad, rad); 
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        repaint();
        rad++;
        if(rad >= 200){
            timer.stop();
        }
    }

推荐答案

So 而不是...

while(rad < 200){
    repaint();
    rad++;
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

你只需要稍微改变一下逻辑......

You simply need to turn the logic around a little...

Timer timer = new Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        rad++;
        if (rad < 200) {
            repaint();
        } else {
            ((Timer)evt.getSource()).stop();
        }
    }
});
timer.start();

基本上,Timer 将充当 Thread.sleep(),但以一种不会破坏 UI 的好方式,但允许您注入执行之间的延迟.每次执行时,您都需要增加您的值,测试停止"条件,否则更新...

Basically, the Timer will act as the Thread.sleep(), but in a nice way that doesn't break the UI, but will allow you to inject a delay between execution. Each time it executes, you need to increment your value, test for the "stop" condition and update otherwise...

查看如何使用摆动计时器和其他 3, 800 个问题关于 SO...

Take a look at How to Use Swing Timers and the other 3, 800 questions on the subject on SO...

这篇关于使用定时器暂停程序执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆