【问题标题】:Using Timer To Pause Program Execution [duplicate]使用计时器暂停程序执行[重复]
【发布时间】:2014-02-20 05:21:03
【问题描述】:

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

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 实现的尝试(如果有误请告诉我):

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();
        }
    }

【问题讨论】:

  • 您仍在 Swing 事件线程上调用 Thread.sleep(...)。我以为我们已经在您的previous similar question 中解决了这个问题。什么给了?
  • “使用定时器暂停程序执行” 暂停究竟是什么,GUI 渲染?图形用户界面做了一些长时间运行的操作?允许用户输入?请注意,while(rad &lt; 200){ ... Thread.sleep(100); 表示在 GUI 中渲染动画时常见的经典错误。
  • 我无法理解如何使用定时器。你曾建议我提供一个简单的、可编译的代码。所以我把它贴在这里。
  • @AndrewThompson:他已经被告知了这一切。
  • @AndrewThompson:是的,我想暂停 GUI 渲染。我知道你在说什么。我只是想要一个关于如何做的例子。

标签: java swing user-interface timer


【解决方案1】:

所以不是...

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

你只需要稍微扭转一下逻辑......

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,但允许您在执行之间注入延迟。每次执行时,您都需要增加您的值,测试“停止”条件,否则更新...

查看How to Use Swing Timers 和其他 3, 800 个关于 SO...主题的问题...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-24
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多