在家学技术实在是不好坚持,心里老是想给自己放个假,但是现在俺又太菜,又想赶赶技术。前天做一个五子棋程序,
遇到了计时器功能的需求,但是这个我没用过,就总结一下吧,不过虽然是会用了,但是和swing的代码整合还是有问题,困扰好久
,到现在也没解决,一气之下,看了三部电影,呵呵。
这个计时器,就是为了使在下五子棋过程中,黑白双方,轮流下的时候,给下棋一方计时,并且要求,时间动态显示在
面板上,就是一秒一秒在走的效果,等这方下了之后,这方的时间停,另一方的表开始走。因为我这个计时器的时间显示是通过
Graphics手绘在面板上显示的,并不是单独一个文本域显示,所以每走一秒要重绘一次面板,功能需求十分的不爽。而Java实现
倒计时,需要递归调用,本来实现秒表就够麻烦了,还要和JFrame整合在一起,JFrame的状态标识属性又多,看着看着就晕了。
尝试个很多次,都没实现。不过timer这个计时器倒是实现了,其实如果那个计时器实现了,这个五子棋就完美了,然后就可以用我想的
广义遍历坐标算法尝试实现人工智能了,就是模拟人机对战。但是这个功能不搞定,实在没心情往下写代码了。好了起因就说到这。
下面给两份源码,一份是基本的,一份是整合JFrame的,不过这个整合和俺的需求不符,所以没用上。
下面是基本类的使用源码:
package
com.sy.game.test;
import java.util.Timer;
import java.util.TimerTask;
public class TimeTask {
public static void main(String[] args) {
TimeTask tTask = new TimeTask();
tTask.timeVoid();
}
public void timeVoid(){
final Timer timer = new Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
System.out.println( " 到点啦! " );
timer.cancel();
}
};
timer.schedule(tt, 3000 );
}
}
import java.util.Timer;
import java.util.TimerTask;
public class TimeTask {
public static void main(String[] args) {
TimeTask tTask = new TimeTask();
tTask.timeVoid();
}
public void timeVoid(){
final Timer timer = new Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
System.out.println( " 到点啦! " );
timer.cancel();
}
};
timer.schedule(tt, 3000 );
}
}
整合的:
/*
* java倒计时器
* shiyang
* */
package com.sy.game.test;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
@SuppressWarnings( " unused " )
public class TimeController extends JFrame implements ActionListener {
private static final long serialVersionUID = 4603262282860990473L ;
private static final int DEFAULT_WIDTH = 200 ;
private static final int DEFAULT_HEIGHT = 100 ;
private static final int width = Toolkit.getDefaultToolkit()
.getScreenSize().width;
private static final int height = Toolkit.getDefaultToolkit()
.getScreenSize().height;
private Container container;
private JButton btn;
private JTextField jtfTime;
private Timer tmr;
public TimeController() {
initComponents();
Timer tmr = new Timer( 1000 , this );
this .tmr = tmr;
setVisible( true );
}
private void initComponents() {
this .setTitle( " SY秒表 " );
this .setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this .setResizable( false );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this .setLocation((width - DEFAULT_WIDTH) / 2 ,
(height - DEFAULT_HEIGHT) / 2 );
jtfTime = new JTextField( " 10 " );
btn = new JButton( " 开始倒计时 " );
container = getContentPane();
JPanel panel = new JPanel();
panel.add(btn);
panel.add(jtfTime);
this .add(panel);
btn.addActionListener( this );
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn) {
jtfTime.setText( " 10 " );
tmr.start();
} else {
int t;
t = Integer.parseInt(jtfTime.getText());
t -- ;
jtfTime.setText( "" + t);
if (t <= 0 ) {
tmr.stop();
}
}
}
public static void main(String[] args) {
TimeController timeController = new TimeController();
}
}
* java倒计时器
* shiyang
* */
package com.sy.game.test;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
@SuppressWarnings( " unused " )
public class TimeController extends JFrame implements ActionListener {
private static final long serialVersionUID = 4603262282860990473L ;
private static final int DEFAULT_WIDTH = 200 ;
private static final int DEFAULT_HEIGHT = 100 ;
private static final int width = Toolkit.getDefaultToolkit()
.getScreenSize().width;
private static final int height = Toolkit.getDefaultToolkit()
.getScreenSize().height;
private Container container;
private JButton btn;
private JTextField jtfTime;
private Timer tmr;
public TimeController() {
initComponents();
Timer tmr = new Timer( 1000 , this );
this .tmr = tmr;
setVisible( true );
}
private void initComponents() {
this .setTitle( " SY秒表 " );
this .setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this .setResizable( false );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this .setLocation((width - DEFAULT_WIDTH) / 2 ,
(height - DEFAULT_HEIGHT) / 2 );
jtfTime = new JTextField( " 10 " );
btn = new JButton( " 开始倒计时 " );
container = getContentPane();
JPanel panel = new JPanel();
panel.add(btn);
panel.add(jtfTime);
this .add(panel);
btn.addActionListener( this );
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn) {
jtfTime.setText( " 10 " );
tmr.start();
} else {
int t;
t = Integer.parseInt(jtfTime.getText());
t -- ;
jtfTime.setText( "" + t);
if (t <= 0 ) {
tmr.stop();
}
}
}
public static void main(String[] args) {
TimeController timeController = new TimeController();
}
}
都可以直接运行。
本文转自施杨博客园博客,原文链接:http://www.cnblogs.com/shiyangxt/archive/2009/01/25/1380815.html,如需转载请自行联系原作者