计时器在java中使用Eclipse [英] Timer in java using Eclipse

查看:320
本文介绍了计时器在java中使用Eclipse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



任何人都可以解释一下(在一个for中)使用定时器重新绘制表单需要做什么?



我正在尝试像时钟那样简单的做一些事情。我需要一个计时器来每秒刷新一次。



如下所示:

  private void activateTimer()
{
ActionListener myAction;
myAction = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
whatever.redraw();
}

};
myTimer = new Timer(1000,myAction);
myTimer.start();
}

当操作必须执行时,我收到错误:

  *线程中的异常AWT-EventQueue-0org.eclipse.swt.SWTException:无效的线程访问* 

这是我收到的完整异常:

 线程中的异常AWT-EventQueue-0org.eclipse.swt.SWTException:无效的线程访问
在org.eclipse.swt.SWT.error(SWT.java:4282)
在org.eclipse.swt.SWT.error(SWT.java:4197)
在org.eclipse.swt.SWT.error(SWT.java:4168)
在org.eclipse.swt.widgets .Widget.error(Widget.java:468)
在org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:359)
在org.eclipse.swt.widgets.Control.redraw (Control.java:2327)
在default.myTimer $ 1.actionPerformed(myTimer.java:97)
在javax.swing.Timer.fireActionPerformed(未知源)
在javax.swing。 Timer $ DoPostEvent.run(Unknown Source)
在java.awt.event.Invocati onEvent.dispatch(未知源)
在java.awt.EventQueue.dispatchEventImpl(未知源)
在java.awt.EventQueue.access $ 000(未知源)
在java.awt.EventQueue $ 3.run(未知源)
在java.awt.EventQueue $ 3.run(未知源)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)
在java.awt.EventQueue.dispatchEvent(未知源)
在java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)
在java.awt.EventDispatchThread。 pumpEventsForFilter(未知源)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)
在java.awt.EventDispatchThread.pumpEvents(未知来源)
在java.awt.EventDispatchThread.pumpEvents(未知来源)
在java.awt.EventDispatchThread.run(未知来源)

任何想法或任何关于刷新屏幕的样品每一秒?



我按照其中一个答案说明,但我仍然收到相同的错误。

解决方案

为什么不使用SWT Display类内置的定时器功能?

  private void activateTimer(final Display display)
{
display.timerExec(
1000,
new Runnable()
public void run(){
whatever.redraw();
//如果要重复:
display.timerExec(1000,this);
}
});
}


I'm trying to do a little program in Java using Eclipse, and I'm a little bit lost.

Could anybody explain me (in a "for dummies way") what do I have to do for repaint a form using a timer?

I'm trying to do something as simple as a clock. I need a timer to repaint it every second.

Something like this:

private void activateTimer()
{
    ActionListener myAction;
    myAction = new ActionListener () 
    { 
        public void actionPerformed(ActionEvent e) 
        { 
           whatever.redraw();
         } 

    };
    myTimer = new Timer(1000, myAction);
    myTimer.start();
}

When the action must be performed, I receive the error:

*Exception in thread "AWT-EventQueue-0" org.eclipse.swt.SWTException: Invalid thread access*

This is the full exception I receive:

Exception in thread "AWT-EventQueue-0" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4282)
    at org.eclipse.swt.SWT.error(SWT.java:4197)
    at org.eclipse.swt.SWT.error(SWT.java:4168)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
    at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:359)
    at org.eclipse.swt.widgets.Control.redraw(Control.java:2327)
    at default.myTimer$1.actionPerformed(myTimer.java:97)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Any idea or any sample about refreshing a screen every second?

I've followed the instructions in one of the answers but I'm still receiving the same error.

解决方案

Why not use the timer functionality that is built into the SWT Display class?

private void activateTimer(final Display display)
{
    display.timerExec(
        1000,
        new Runnable() {
            public void run() {
                whatever.redraw();
                // If you want it to repeat:
                display.timerExec(1000, this);
            }
        });
}

这篇关于计时器在java中使用Eclipse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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