java秒表小程序编写
界面自定,能获取时间,一定有“开始”和“停止”按钮来控制时间,谢谢!!!有高手能帮我解决下吗?最好代码简单点的不要太难是有秒表功能,不是获取时间...
界面自定,能获取时间,一定有“开始”和“停止”按钮来控制时间,谢谢!!!有高手能帮我解决下吗?最好代码简单点的不要太难
是有秒表功能,不是获取时间 展开
是有秒表功能,不是获取时间 展开
1个回答
展开全部
收藏的一个小程序。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
public class TimerApplet extends JApplet implements ActionListener{
private JLabel lblTime=new JLabel("时间");
private JButton btnStart=new JButton("开始");
private JButton btnEnd=new JButton("结束");
private long startTime=System.currentTimeMillis();
private boolean isRun=false;
private NumberFormat numberFormat=new DecimalFormat("0.000");
public TimerApplet(){
this.setLayout(new FlowLayout());
this.add(lblTime);
this.add(btnStart);
this.add(btnEnd);
btnStart.addActionListener(this);
btnEnd.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnStart){
class TimerThread implements Runnable{
public void run() {
isRun=true;
startTime=System.currentTimeMillis();
while(isRun){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double time=(System.currentTimeMillis()-startTime);
lblTime.setText(numberFormat.format(time/1000));
}
}
}
new Thread(new TimerThread()).start();
}
if(e.getSource()==btnEnd){
isRun=false;
}
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
public class TimerApplet extends JApplet implements ActionListener{
private JLabel lblTime=new JLabel("时间");
private JButton btnStart=new JButton("开始");
private JButton btnEnd=new JButton("结束");
private long startTime=System.currentTimeMillis();
private boolean isRun=false;
private NumberFormat numberFormat=new DecimalFormat("0.000");
public TimerApplet(){
this.setLayout(new FlowLayout());
this.add(lblTime);
this.add(btnStart);
this.add(btnEnd);
btnStart.addActionListener(this);
btnEnd.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnStart){
class TimerThread implements Runnable{
public void run() {
isRun=true;
startTime=System.currentTimeMillis();
while(isRun){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double time=(System.currentTimeMillis()-startTime);
lblTime.setText(numberFormat.format(time/1000));
}
}
}
new Thread(new TimerThread()).start();
}
if(e.getSource()==btnEnd){
isRun=false;
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询