请大家帮我看看这个秒表程序,为神马秒表的速度会快??
import javax.swing.*;
import java.awt.event.*;public class SL111 extends JFrame implements ActionListener{

private JPanel jp=new JPanel();

private JButton start=new JButton("start");
private JButton pause=new JButton("stop");
private JButton reset=new JButton("reset");
private JLabel display=new JLabel("00:00:00");

private Timer t;

int onehundredsecond=0,second=0,minute=0;
String str1,str2,str3,strall;

public SL111(){
super("Timer");

str1="00";
str2="00";
str2="00";
strall="00:00:00";

setBounds(200,200,400,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);

jp.add(display);

jp.add(start);
jp.add(pause);
jp.add(reset);

t=new Timer(10,this);

start.addActionListener(this);
pause.addActionListener(this);
reset.addActionListener(this);
t.addActionListener(this);
setContentPane(jp);
}

public static void main(String[] args){
SL111 s=new SL111();
s.show(true);
}

public void actionPerformed(ActionEvent e) {
Object ob=e.getSource();
if(ob==t){
t.stop();
onehundredsecond++;
if(onehundredsecond==100){
second++;
onehundredsecond=0;
}
if(second==60){
minute++;
second=0;
}
if(minute==99){
t.stop();
onehundredsecond=0;
second=0;
minute=0;
display.setText("00:00:00");
}

if(onehundredsecond<10){
str1="0"+String.valueOf(onehundredsecond);
}
else{
str1=String.valueOf(onehundredsecond);
}
if(second<10){
str2="0"+String.valueOf(second);
}
else{
str2=String.valueOf(second);
}
if(minute<10){
str3="0"+String.valueOf(minute);
}
else{
str3=String.valueOf(minute);
}
strall=str3+":"+str2+":"+str1;
display.setText(strall);
t.start();
}
if(ob==start){
t.start();
}
if(ob==pause){
t.stop();
}
if(ob==reset){
t.stop();
onehundredsecond=0;
second=0;
minute=0;
display.setText("00:00:00");
}
}
}

解决方案 »

  1.   

    if(onehundredsecond==100){
    second++;
    onehundredsecond=0;
    }onhundredsecond应该是1000的时候second才加吧
    1s=1000ms
      

  2.   


    //////////////////////////////////////////////////////////////////////////////
    class TimerRunnable implements Runnable {
        private JLabel jLabel;
        private int hour, minute, second, millisecond;    public TimerRunnable(int hour, int minute, int second, int millisecond,
        JLabel jLabel) {
    this.hour = hour;
    this.minute = minute;
    this.second = second;
    this.millisecond = millisecond;
    this.jLabel = jLabel;
    //new Thread(this).start();
        }    public void run() {
    for (; true; millisecond++) {
        if (millisecond >= 100) {
    second++;
    millisecond = 0;
        }
        if (second >= 60) {
    minute++;
    second = 0;
        }
        if (minute >= 60) {
    hour++;
    minute = 0;
        }
        try {
    Thread.sleep(10);//10毫秒刷新一次
        } catch (Exception f) {
        }
        jLabel.setText("" + hour + ":" + minute + ":" + second + ":"
        + millisecond);
    }
        }    public int getHour() {
    return hour;
        }    public int getMinute() {
    return minute;
        }    public int getSecond() {
    return second;
        }    public int getMillisecond() {
    return millisecond;
        }    public void setHour(int hour) {
    this.hour = hour;
        }    public void setMinute(int minute) {
    this.minute = minute;
        }    public void setSecond(int second) {
    this.second = second;
        }    public void setMillisecond(int millisecond) {
    this.millisecond = millisecond;
        }
    }