java计时器该怎么写
有两个要求1.精确到毫秒或百分之一秒2.有个按钮开始和停止能不能实际上显示时间开始前先设时间为0...
有两个要求
1.精确到毫秒或百分之一秒
2.有个按钮开始和停止
能不能实际上显示时间 开始前先设时间为0 展开
1.精确到毫秒或百分之一秒
2.有个按钮开始和停止
能不能实际上显示时间 开始前先设时间为0 展开
5个回答
展开全部
闲的D疼,给你写了个,能满足你的需求 觉得可以的话 加点分呗 嘿嘿
开始没注意 确实不准 现在基本上OK了 因为你要精确到0.01秒,所以只能这样了 总会有点点误差的
Thread.sleep(9); 的意思是 每隔9毫秒循环一次,但是每循环一次都有语句需要执行,而语句执行是需要时间的,所以就有了误差。 因为这个方法里面只能传long型数据,不能传小数,就导致了不准 估计要准的话 是9.几 呵呵
你可以自己稍微改下代码,让循环里面的语句变少,这样的话会准些
package test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestCounter implements Runnable, ActionListener {
private JLabel counter;
private int hour = 0, min = 0, ss = 0, mm = 0;
private String h, m, s, mmm;
private Thread tt;
private int status = 0;
public TestCounter() {
JFrame main = new JFrame("我的计时器");
main.setLayout(new BorderLayout());
JPanel buttons = new JPanel();
JButton start = new JButton("start");
JButton end = new JButton("end");
JButton clear = new JButton("clear");
start.addActionListener(this);
end.addActionListener(this);
clear.addActionListener(this);
buttons.add(start);
buttons.add(end);
buttons.add(clear);
JPanel view = new JPanel();
counter = new JLabel("00:00:00-00");
view.add(counter);
main.add(buttons, BorderLayout.NORTH);
main.add(view);
main.pack();
main.setLocation(300, 300);
main.setVisible(true);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void run() {
for (;;) {
if (status == 1) {
mm++;
if (mm > 99) {
mm = 0;
ss++;
}
if (ss > 59) {
ss = 0;
min++;
}
if (min > 59) {
min = 0;
hour++;
}
h = hour + "";
m = min + "";
s = ss + "";
mmm = mm + "";
if (mm <= 9) {
mmm = "0" + mm;
}
if (ss <= 9) {
s = "0" + ss;
}
if (min <= 9) {
m = "0" + min;
}
if (hour <= 9) {
h = "0" + hour;
}
counter.setText(h + ":" + m + ":" + s + "-" + mmm);
}
try {
Thread.sleep(9);
} catch (Exception e) {
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if ("start".endsWith(str)) {
status = 1;
if (tt == null) {
tt = new Thread(this);
tt.start();
}
} else if ("end".endsWith(str)) {
status = 0;
} else if ("clear".endsWith(str)) {
hour = 0;
min = 0;
ss = 0;
mm = 0;
counter.setText("00:00:00-00");
}
}
public static void main(String[] args) {
new TestCounter();
}
}
开始没注意 确实不准 现在基本上OK了 因为你要精确到0.01秒,所以只能这样了 总会有点点误差的
Thread.sleep(9); 的意思是 每隔9毫秒循环一次,但是每循环一次都有语句需要执行,而语句执行是需要时间的,所以就有了误差。 因为这个方法里面只能传long型数据,不能传小数,就导致了不准 估计要准的话 是9.几 呵呵
你可以自己稍微改下代码,让循环里面的语句变少,这样的话会准些
package test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestCounter implements Runnable, ActionListener {
private JLabel counter;
private int hour = 0, min = 0, ss = 0, mm = 0;
private String h, m, s, mmm;
private Thread tt;
private int status = 0;
public TestCounter() {
JFrame main = new JFrame("我的计时器");
main.setLayout(new BorderLayout());
JPanel buttons = new JPanel();
JButton start = new JButton("start");
JButton end = new JButton("end");
JButton clear = new JButton("clear");
start.addActionListener(this);
end.addActionListener(this);
clear.addActionListener(this);
buttons.add(start);
buttons.add(end);
buttons.add(clear);
JPanel view = new JPanel();
counter = new JLabel("00:00:00-00");
view.add(counter);
main.add(buttons, BorderLayout.NORTH);
main.add(view);
main.pack();
main.setLocation(300, 300);
main.setVisible(true);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void run() {
for (;;) {
if (status == 1) {
mm++;
if (mm > 99) {
mm = 0;
ss++;
}
if (ss > 59) {
ss = 0;
min++;
}
if (min > 59) {
min = 0;
hour++;
}
h = hour + "";
m = min + "";
s = ss + "";
mmm = mm + "";
if (mm <= 9) {
mmm = "0" + mm;
}
if (ss <= 9) {
s = "0" + ss;
}
if (min <= 9) {
m = "0" + min;
}
if (hour <= 9) {
h = "0" + hour;
}
counter.setText(h + ":" + m + ":" + s + "-" + mmm);
}
try {
Thread.sleep(9);
} catch (Exception e) {
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if ("start".endsWith(str)) {
status = 1;
if (tt == null) {
tt = new Thread(this);
tt.start();
}
} else if ("end".endsWith(str)) {
status = 0;
} else if ("clear".endsWith(str)) {
hour = 0;
min = 0;
ss = 0;
mm = 0;
counter.setText("00:00:00-00");
}
}
public static void main(String[] args) {
new TestCounter();
}
}
2010-08-10
展开全部
SWING ?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是想有人把代码贴上来?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TimeCount extends JFrame implements ActionListener{
ThreadCount tc=new ThreadCount(this);
Thread thread=new Thread(tc);
JPanel panelN=new JPanel(),panelC=new JPanel();
JLabel label=new JLabel("计时器");
JButton butnStart=new JButton("开始");
boolean toEnd;
public TimeCount() {
setBounds(100,100,300,300);
setVisible(true);
label.setFont(new Font(null,Font.BOLD,22));
panelN.add(label);
add(panelN,BorderLayout.NORTH);
panelC.add(butnStart);
add(panelC,BorderLayout.CENTER);
butnStart.addActionListener(this);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==butnStart){
if(!thread.isAlive()){
thread=new Thread(tc);
thread.start();
}else {
toEnd=true;
}
}
}
public static void main(String[] args) {
new TimeCount();
}
}
class ThreadCount implements Runnable{
TimeCount lc;
public ThreadCount(TimeCount lc) {
super();
this.lc = lc;
}
public void run() {
int i=1;
while(true){
if(lc.toEnd){
lc.toEnd=false;
lc.butnStart.setText("开始");
return;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO: handle exception
}
i+=2;
int min=i/60000;
int second=(i%60000)/1000;
int mm=i%1000;
String show="";
if(min>0)
show+=min+":";
if(second>0)
show+=second+".";
show+=mm;
lc.label.setText(show);
}
}
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TimeCount extends JFrame implements ActionListener{
ThreadCount tc=new ThreadCount(this);
Thread thread=new Thread(tc);
JPanel panelN=new JPanel(),panelC=new JPanel();
JLabel label=new JLabel("计时器");
JButton butnStart=new JButton("开始");
boolean toEnd;
public TimeCount() {
setBounds(100,100,300,300);
setVisible(true);
label.setFont(new Font(null,Font.BOLD,22));
panelN.add(label);
add(panelN,BorderLayout.NORTH);
panelC.add(butnStart);
add(panelC,BorderLayout.CENTER);
butnStart.addActionListener(this);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==butnStart){
if(!thread.isAlive()){
thread=new Thread(tc);
thread.start();
}else {
toEnd=true;
}
}
}
public static void main(String[] args) {
new TimeCount();
}
}
class ThreadCount implements Runnable{
TimeCount lc;
public ThreadCount(TimeCount lc) {
super();
this.lc = lc;
}
public void run() {
int i=1;
while(true){
if(lc.toEnd){
lc.toEnd=false;
lc.butnStart.setText("开始");
return;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO: handle exception
}
i+=2;
int min=i/60000;
int second=(i%60000)/1000;
int mm=i%1000;
String show="";
if(min>0)
show+=min+":";
if(second>0)
show+=second+".";
show+=mm;
lc.label.setText(show);
}
}
}
参考资料: 还有其他问题的话,给我发百度消息
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询