一個小小的移動(利用Timer及TimerTask)
2006-04-11 11:23
211 查看
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame1
extends JFrame
{
private JButton jButton1 = new JButton();
//生成一個定時器
Timer moveTimer = new Timer();
int x;
int y;
int frameLeft;
int frameRight;
public Frame1()
{
try
{
jbInit();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x=jButton1.getX();
y=jButton1.getY();
frameLeft=this.getX()+this.getWidth();
frameRight=this.getY()+this.getHeight();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void move()
{
Date date=new Date(System.currentTimeMillis()+1000);
//使用內部類執行
moveTimer.schedule(new TimerTask()
{
public void run()
{
x+=40;
System.out.println("X:"+x);
System.out.println("frameLeft:"+frameLeft);
//如果此時已經到了最右邊,那就從左邊開始循環
if(x>=frameLeft)
{
//x=frameX-jButton1.WIDTH;
x=frameLeft-x;
}
jButton1.setLocation(x,y);
}
},date,1000);
}
private void jbInit()
throws Exception
{
this.getContentPane().setLayout(null);
this.setSize(new Dimension(400, 300));
jButton1.setText("jButton1");
jButton1.setBounds(new Rectangle(70, 50, 95, 45));
this.getContentPane().add(jButton1, null);
}
public static void main(String[] arg)
{
Frame1 frame=new Frame1();
frame.setVisible(true);
frame.move();
}
}
import java.awt.Rectangle;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Frame1
extends JFrame
{
private JButton jButton1 = new JButton();
//生成一個定時器
Timer moveTimer = new Timer();
int x;
int y;
int frameLeft;
int frameRight;
public Frame1()
{
try
{
jbInit();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x=jButton1.getX();
y=jButton1.getY();
frameLeft=this.getX()+this.getWidth();
frameRight=this.getY()+this.getHeight();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void move()
{
Date date=new Date(System.currentTimeMillis()+1000);
//使用內部類執行
moveTimer.schedule(new TimerTask()
{
public void run()
{
x+=40;
System.out.println("X:"+x);
System.out.println("frameLeft:"+frameLeft);
//如果此時已經到了最右邊,那就從左邊開始循環
if(x>=frameLeft)
{
//x=frameX-jButton1.WIDTH;
x=frameLeft-x;
}
jButton1.setLocation(x,y);
}
},date,1000);
}
private void jbInit()
throws Exception
{
this.getContentPane().setLayout(null);
this.setSize(new Dimension(400, 300));
jButton1.setText("jButton1");
jButton1.setBounds(new Rectangle(70, 50, 95, 45));
this.getContentPane().add(jButton1, null);
}
public static void main(String[] arg)
{
Frame1 frame=new Frame1();
frame.setVisible(true);
frame.move();
}
}
相关文章推荐
- 利用Timer和TimerTask结合定时执行一个方法
- [JavaME]利用java.util.TimerTask来做Splash Screen的N种方法
- java中计时器的用法Timer和TimerTask的用法__java中利用Timer与TImerTask 计时器间隔执行任务
- android AppWidget的使用以及利用TimerTask实现widget的定时更新
- 如何利用TimerTask与Handler延迟多线程Android源代码
- android AppWidget的使用以及利用TimerTask实现widget的定时更新
- Spring利用TimerTask定时控制
- ant中利用macrodef来定义可重用的task
- How to create a task in the openwrt system, the timer to switch WiFi power。
- Timer与TimerTask的真正原理
- j2me 使用Timer和TimerTask来实现多线程
- 利用Timer及TimerTask实现游戏LOGO闪屏及煮鸡蛋闹钟程序
- 《Java源码分析》:Timer/TimerTask
- Linux下利用posix线程库设计自己的应用层timer定时器
- Java中的Timer和TimerTask来实现多线程
- android TimerTask 的简单应用,以及java.lang.IllegalStateException: TimerTask is scheduled already错误的解决方法
- Spring整合TimerTask实现定时任务调度
- TimerTask is scheduled already
- TimerTask 延迟执行
- Timer运行多个TimeTask