import java.awt.*;import java.awt.event.*;public class StopWatchFrame {
public Frame getF() {
return f;
} public void setF(Frame f) {
this.f = f;
} public String getMs() {
return ms + ""; } public void setMs(int ms) {
this.ms = ms; } public int getSs() {
return ss;
} public void setSs(int ss) {
this.ss = ss;
} public void setT(Thread t) {
this.t = t;
} public Thread getT() {
return t;
} private Frame f;
private int ms;// hao miao
private int ss;// miao
private TextField tf;
private Font font;
private Thread t;
private Boolean flag;
Button b2 = new Button("End");
Button b1 = new Button("Begin"); public StopWatchFrame() { ms = 0;
ss = 0;
flag = false;
t = new Thread();
f = new Frame("Sexxx's Stopwatch");
tf = new TextField();
font = new Font("微软雅黑", Font.PLAIN, 100);
tf.setFont(font);
tf.setText("00:00");
tf.setSize(260, 120);
tf.setLocation(60, 60);
b1.setSize(50, 20);
b1.setLocation(400, 200);
b2.setSize(50, 20);
b2.setLocation(400, 250);
b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
flag = true;
t.start();
setTime();
} });
b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
flag = false;
t = null;
t.stop();
} });
f.setLayout(null);
f.setBounds(120, 120, 500, 300);
f.setBackground(Color.black);
f.add(tf);
f.add(b1);
f.add(b2);
f.setVisible(true);
f.setResizable(false);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Window w = e.getWindow();
w.dispose();
}
}); } public void setTime() {
while (flag) {
ms++;
if (ms % 100 == 0) {
ms = 0;
ss++;
}
tf.setText((ss + "") + ":" + (ms + ""));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
} } } public static void main(String[] args) {
new StopWatchFrame(); }}
这是源代码,我觉得还是比较幼稚的。求高手帮忙改改。
主要问题就是只能开始计时,然后就不能停止了。
其实我想用键盘的,但是也不行,就是按一下空格开始,再按一下结束。
小弟水平有限,惭愧。
问题出在Thread里没有run方法啊
public StopWatchFrame() {
ms = 0;
ss = 0;
flag = false;
//
t = new Thread(){
@Override
public void run(){
setTime();
}
};
f = new Frame("Sexxx's Stopwatch");------------------------
然后
public void actionPerformed(ActionEvent e) {
flag = true;
t.start();
setTime();
}
去掉setTime();OK。
虽然还有很多问题,但是能停止了。
最后提醒下,不要用stop方法。其它的就自己去完善吧。
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class StopWatchFrame extends Frame { private Boolean flag = true;
private int ms;
private int ss;
private TimeThread t = new TimeThread();
private final Object lock = new Object();
private TextField tf;
private Button begin;
private Button end;
private Button restart; public StopWatchFrame() {
textFieldInit();
buttonInit();
frameInit();
t.start();
} private void frameInit() {
setLayout(null);
setBounds(120, 120, 500, 300);
setBackground(Color.black);
setVisible(true);
setResizable(false);
add(tf);
add(begin);
add(end);
add(restart); addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} private void textFieldInit() {
tf = new TextField();
tf.setFont(new Font(null, Font.PLAIN, 100));
tf.setText("00:00");
tf.setSize(260, 120);
tf.setLocation(60, 60);
} private void buttonInit() {
begin = new Button("Begin");
end = new Button("End");
restart = new Button("Restart"); begin.setSize(50, 20);
end.setSize(50, 20);
restart.setSize(50, 20); begin.setLocation(400, 150);
end.setLocation(400, 200);
restart.setLocation(400, 250); begin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = false;
synchronized (lock) {
lock.notify();
}
}
}); end.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = true;
}
}); restart.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
flag = true;
tf.setText("00:00");
ms = 0;
ss = 0;
}
});
} private void setTime() throws InterruptedException {
while (true) {
synchronized (lock) {
if (flag) {
lock.wait();
}
}
ms++;
if (ms % 100 == 0) {
ms = 0;
ss++;
}
tf.setText(ss + ":" + ms );
Thread.sleep(9);
}
} class TimeThread extends Thread { @Override
public void run() {
try {
setTime();
} catch (InterruptedException ex) {
//Logger.getLogge........
}
}
} public static void main(String[] args) {
new StopWatchFrame();
}
}
* http://topic.csdn.net/u/20111202/13/fd63337a-f619-4c58-8b34-6f49afb81526.html?80231
* name: StopWatchFrame.java
*/
package download; //YYm addimport java.awt.*;
import java.awt.event.*;
import java.util.*;//====
//======================= class StopWatchFrame
//====
public class StopWatchFrame
{
public Frame getF()
{
return f;
} public void setF(Frame f)
{
this.f = f;
} public int getMs()
{
return ms;
} public void setMs(int ms)
{
this.ms = ms;
} public int getSs()
{
return ss;
} public void setSs(int ss)
{
this.ss = ss;
} public void setT(Thread t)
{
this.t = t;
} public Thread getT()
{
return t;
} public TextField getTf()
{
return tf;
} public boolean getFlag()
{
return flag;
} public long getMst()
{
return mst;
} public long getStopTimeLength()
{
return stopTimeLength;
} public int getMins()
{
return mins;
} public void setMins(int hs)
{
this.mins=mins;
} private Frame f;
private int ms; // millisecond
private int ss; // second
private int mins; //minute
private long mst; //current time add old time.
private long stopTimeLength=0; //按下暂停时的计时毫秒数.
private TextField tf;
private Font font;
private Thread t;
private boolean flag;
Button b2 = new Button("End");
Button b1 = new Button("Begin"); //----------------- constructor
public StopWatchFrame()
{
ms = 0;
ss = 0;
flag = false;
//t = new Thread();
f = new Frame("Sexxx's Stopwatch");
tf = new TextField();
font = new Font("微软雅黑", Font.PLAIN, 100);
tf.setFont(font);
tf.setText("00:00:00");
tf.setSize(410, 120); //放大了一些.
tf.setLocation(60, 60);
b1.setSize(50, 20);
b1.setLocation(400, 200);
b2.setSize(50, 20);
b2.setLocation(400, 250); b1.addActionListener(new ActionListener() //anonymous class
{
public void actionPerformed(ActionEvent e)
{
stopTimeLength=mins*60*1000+ss*1000+ms*10; //此句是为了使你连续按"begin"没有影响。
mst=System.currentTimeMillis(); //the begin time after press b1
flag = true;
}
}); b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
flag = false; //使计时暂停。
stopTimeLength=mins*60*1000+ss*1000+ms*10; //暂停后记录当时计时的毫秒数。
} }); f.setLayout(null);
f.setBounds(120, 120, 500, 300);
f.setBackground(Color.black);
f.add(tf);
f.add(b1);
f.add(b2);
f.setVisible(true);
f.setResizable(false); f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
Window w = e.getWindow();
w.dispose();
System.exit(0); //程序退出。
}
});
}
//end constructor public static void main(String[] args)
{
StopWatchFrame swf=new StopWatchFrame();
StopWatch sw=new StopWatch(swf);
new Thread(sw).start(); }}
//================== end class StopWatchFrame//====
//================== class StopWatch
//====
class StopWatch implements Runnable
{
StopWatchFrame swf;
long t;
long difference;
//-------------- constructor
public StopWatch(StopWatchFrame swf)
{
this.swf=swf;
} //-------------- 线程体
public void run()
{
while(true)
{
while (swf.getFlag())
{
t=System.currentTimeMillis(); //current time
difference=t-swf.getMst()+swf.getStopTimeLength(); //time from begein.
difference=difference/10;
swf.setMins((int)(difference/(60*100)));
swf.setSs((int)((difference/100)%60));
swf.setMs((int)difference%100); swf.getTf().setText((swf.getMins()+"")+":"+(swf.getSs() + "") + ":" + (swf.getMs() + ""));
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
e.printStackTrace();
}//end try/catch.
}//end swf.getFlag()
}//end while
}//end run
}//end class StopWatch
改了几点,你试试.
1 那个计时不准,我修改了,取系统时间.
2 程序无法退出,我加了一句,System.exit(0)
3 加了一个实现了Runnable接口的类,实现秒表线程。
4 按"end"秒表暂停,按"begin"继续开始。(时间延续)。
5 时间扩大到分。还有许多不足的地方,希望能对你有所帮助。