随笔-0  评论-0  文章-2  trackbacks-0
                         今天即将考试就是看不进书,。没事就顺便写了关于一个java的打地鼠的程序,用到了定时器的东西,就写了点关于计时器的打地鼠。
           package test;
import java.awt.*;
public class TimerMouse extends JDialog implements ActionListener{
private final JPanel contentPanel = new JPanel();
    private JButton btnStart;
    private static JButton btnMouse;
    private  Timer timer;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
TimerMouse dialog = new TimerMouse();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public void init() {
btnStart = new JButton("\u5F00\u59CB");
btnStart.setBounds(103, 278, 93, 23);
btnStart.addActionListener(this);
getContentPane().add(btnStart);
   btnMouse = new JButton("Mouse");
btnMouse.setBounds(131, 57, 93, 23);
getContentPane().add(btnMouse);
}
/**
* Create the dialog.
*/
public TimerMouse() {
setBounds(100, 100, 389, 365);
getContentPane().setLayout(null);
init();
 
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
timer = new Timer();
 
timer.schedule(new MyTask(), 10, 1000);
}
static class MyTask extends java.util.TimerTask{
       @Override
       public void run() {
        int x=(int)(Math.random()*100);
        int y=(int)(Math.random()*100);
        System.out.println(x+"");
        System.out.println(y+"");
       
       
        btnMouse.setLocation(new Point(x,y));
       }
   }
}
posted on 2011-06-24 16:14 ownWell 阅读(495) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问