在JFrame Java中闪烁 [英] Blinking in JFrame Java

查看:891
本文介绍了在JFrame Java中闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在做一个线程来更新JFrame上的一个球,所以我重新绘制屏幕...然后画球更新其位置..然后再画画面...绘制球和相同的周期...这里是代码

Hello guys I am doing a thread to update a ball over JFrame so I repaint the screen... and then paint the ball update its position .. and then draw the screen again ... draw the ball and the same cycle ... here is the code

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
   Thread t = new Thread()
   {
     public void run()
     {
         while(true)
         {
             repaint();
             b2.update(ob,2);
             b2.paint(ob.getGraphics());

             b2.setT(b2.getT() + 1);
             try {
                 Thread.sleep(50);
             } catch (InterruptedException ex) {
                 System.out.println("Error in Sleeping");
             }

         }
     }
   };

   t.start();
}

但问题是我看不到球...

but the problem is that I don't see the ball... the paint of the screen always overrides the ball and the ball is like down under the Jframe ..

推荐答案

如果你想拥有动画在Swing中,建议使用的类是 javax.swing.Timer 。此类允许您定期对事件分派主题执行操作。

If you want to have animations in Swing, the recommended class to use is the javax.swing.Timer . This class allows you to perform operations on the Event Dispatch Thread at regular intervals.


  • Swing 计时器教程

  • a href =http://stackoverflow.com/q/9849950/1076463>动画示例发布在这里(在Swing wiki在SO btw链接)

  • The Swing Timer tutorial
  • An animation example posted here on SO (which is linked in the Swing wiki here on SO btw)

这篇关于在JFrame Java中闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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