怎么用JAVA 实现秒表功能 给我一点思路
1个回答
2017-02-06 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class T extends JFrame implements Runnable,ActionListener
{
private int minutes;
private static double seconds;
private Thread mb;
private boolean isRun;
private Button start,stop,reset;
private Label desplay;
T(){
start=new Button("开始");
stop=new Button("停止");
reset=new Button("重置");
desplay=new Label();
this.setLayout(null);
desplay.setBounds(0,30,200,50);
start.setBounds(0,80,50,50);
stop.setBounds(60,80,50,50);
reset.setBounds(120,80,50,50);
refresh();
add(desplay);
add(start);
start.addActionListener(this);
stop.addActionListener(this);
reset.addActionListener(this);
add(stop);
add(reset);
setSize(300,200);
show();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args)
{
new T();
}
public void run(){
while(isRun){
try{
seconds+=0.001d;
Thread.sleep(1);
if(seconds>59d){
seconds=0d;
minutes=minutes+1;
System.out.print(minutes);
}
refresh();
}
catch(Exception e){}
}
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("开始")){
startCount();
}
else if(e.getActionCommand().equals("停止")){
stopCount();
}
else{
reset();
}
}
public void refresh(){
desplay.setText(toString());
}
public void startCount(){
if(!isRun){
isRun=true;
mb=new Thread(this);
mb.start();
}
}
public void stopCount(){
if(isRun){
isRun=false;
}
}
public void reset(){
isRun=false;
minutes=0;
seconds=0d;
refresh();
}
public int getMinute(){
return minutes;
}
public double getSecond(){
return seconds;
}
public int getTotalTime(){
return minutes+(int)(seconds*1000);
}
public String toString(){
return ""+minutes+":"+(int)seconds+":"+(int)((seconds-(int)seconds)*1000);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class T extends JFrame implements Runnable,ActionListener
{
private int minutes;
private static double seconds;
private Thread mb;
private boolean isRun;
private Button start,stop,reset;
private Label desplay;
T(){
start=new Button("开始");
stop=new Button("停止");
reset=new Button("重置");
desplay=new Label();
this.setLayout(null);
desplay.setBounds(0,30,200,50);
start.setBounds(0,80,50,50);
stop.setBounds(60,80,50,50);
reset.setBounds(120,80,50,50);
refresh();
add(desplay);
add(start);
start.addActionListener(this);
stop.addActionListener(this);
reset.addActionListener(this);
add(stop);
add(reset);
setSize(300,200);
show();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args)
{
new T();
}
public void run(){
while(isRun){
try{
seconds+=0.001d;
Thread.sleep(1);
if(seconds>59d){
seconds=0d;
minutes=minutes+1;
System.out.print(minutes);
}
refresh();
}
catch(Exception e){}
}
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("开始")){
startCount();
}
else if(e.getActionCommand().equals("停止")){
stopCount();
}
else{
reset();
}
}
public void refresh(){
desplay.setText(toString());
}
public void startCount(){
if(!isRun){
isRun=true;
mb=new Thread(this);
mb.start();
}
}
public void stopCount(){
if(isRun){
isRun=false;
}
}
public void reset(){
isRun=false;
minutes=0;
seconds=0d;
refresh();
}
public int getMinute(){
return minutes;
}
public double getSecond(){
return seconds;
}
public int getTotalTime(){
return minutes+(int)(seconds*1000);
}
public String toString(){
return ""+minutes+":"+(int)seconds+":"+(int)((seconds-(int)seconds)*1000);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询