请大家帮我看看这个秒表程序,为神马秒表的速度会快??
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");
}
}
}
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");
}
}
}
解决方案 »
- 求解:关于RuntimeException的理解
- Java如何实现二叉排序
- Thread.currentThread().getContextClassLoader().loadClass有什么用?
- 多线程问题,请教!
- 简单问题,举手之劳:找不到符号Protection??
- Java实现序列化的底层机制是什么?
- 编码新发现
- java socket采用多线程接收数据出现CPU使用上升的问题
- 如何才能顺利的通过SCJP?高分!!!!!!!!!!!
- 请问在java中arguments这个单词应如何译?
- spring在非容器下获取bean后,关闭程序后,修改了配置文件,重启程序后,获取的bean还是之前的,这是怎样回事
- Unicode转换为GB问题
second++;
onehundredsecond=0;
}onhundredsecond应该是1000的时候second才加吧
1s=1000ms
//////////////////////////////////////////////////////////////////////////////
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;
}
}