写一个计时器 JAVA代码是什么?

用JAVA写一个计时器... 用JAVA写一个计时器 展开
 我来答
匿名用户
2013-07-01
展开全部
应该用线程里面的Timer来控制  package com.sy.game.test;
  import java.util.Timer;
  import java.util.TimerTask;
  public class TimeTask {
  public static void main(String[] args) {
  TimeTask tTask=new TimeTask();
  tTask.timeVoid();
  }
  public void timeVoid(){
  final Timer timer = new Timer();
  TimerTask tt=new TimerTask() {
  @Override
  public void run() {
  System.out.println("到点啦!");
  timer.cancel();
  }
  };
  timer.schedule(tt, 3000);
  }
  }
  整合的:
  /*
  * java倒计时器
  * shiyang
  * */
  package com.sy.game.test;
  import java.awt.Container;
  import java.awt.FlowLayout;
  import java.awt.Toolkit;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  import javax.swing.Timer;
  @SuppressWarnings("unused")
  public class TimeController extends JFrame implements ActionListener {
  private static final long serialVersionUID = 4603262282860990473L;
  private static final int DEFAULT_WIDTH = 200;
  private static final int DEFAULT_HEIGHT = 100;
  private static final int width = Toolkit.getDefaultToolkit()
  .getScreenSize().width;
  private static final int height = Toolkit.getDefaultToolkit()
  .getScreenSize().height;
  private Container container;
  private JButton btn;
  private JTextField jtfTime;
  private Timer tmr;
  public TimeController() {
  initComponents();
  Timer tmr = new Timer(1000, this);
  this.tmr = tmr;
  setVisible(true);
  }
  private void initComponents() {
  this.setTitle("SY秒表");
  this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  this.setResizable(false);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setLocation((width - DEFAULT_WIDTH) / 2,
  (height - DEFAULT_HEIGHT) / 2);
  jtfTime = new JTextField("10");
  btn = new JButton("开始倒计时");
  container = getContentPane();
  JPanel panel = new JPanel();
  panel.add(btn);
  panel.add(jtfTime);
  this.add(panel);
  btn.addActionListener(this);
  }
  public void actionPerformed(ActionEvent ae) {
  if (ae.getSource() == btn) {
  jtfTime.setText("10");
  tmr.start();
  } else {
  int t;
  t = Integer.parseInt(jtfTime.getText());
  t--;
  jtfTime.setText("" + t);
  if (t <= 0) {
  tmr.stop();
  }
  }
  }
  public static void main(String[] args) {
  TimeController timeController = new TimeController();
  }
  }
匿名用户
2013-07-01
展开全部
package simpleTimer;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SimpleTimer extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JFrame f1;
JPanel p1;
JFileChooser jfc1;
JLabel l1,l2,l3,l4;
TextField t1,t2,t3;
JButton begin,end,quit;
Long beginTime,endTime;
Float resultTime;

public void display(){
f1=new JFrame("时间计时器");

p1=new JPanel();

l1=new JLabel("开始时间");
p1.add(l1);

t1=new TextField(20);
t1.setEditable(false);
p1.add(t1);

l2=new JLabel("结束时间");
p1.add(l2);

t2=new TextField(20);
t2.setEditable(false);
p1.add(t2);

l3=new JLabel("总共用时");
p1.add(l3);

t3=new TextField(20);
t3.setEditable(false);
p1.add(t3);

begin=new JButton("[开始]");
end=new JButton("[结果]");
end.setEnabled(false);
quit=new JButton("[退出]");
begin.addActionListener(this);
end.addActionListener(this);
quit.addActionListener(this);
p1.add(begin);
p1.add(end);
p1.add(quit);

f1.add(p1);
f1.setSize(250, 300);
f1.setVisible(true);
f1.setLocation(350, 255);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==begin){
t1.setText(new Date().toLocaleString());
t2.setText("");
t3.setText("");
beginTime=System.nanoTime();
for(int i=0;i<1000000;i++){
System.out.println(Math.random());
}
endTime=System.nanoTime();
resultTime = (float)(endTime-beginTime)/1000000000;
t2.setText(new Date().toLocaleString());
t3.setText(resultTime.toString());
end.setEnabled(true);
}
if(e.getSource()==end){
t2.setText(new Date().toLocaleString());
endTime=System.nanoTime();
resultTime = (float)(endTime-beginTime)/1000000000;
t3.setText(resultTime.toString());
}

if(e.getSource()==quit){
System.exit(0);
}
}

public static void main(String args[]){
SimpleTimer st = new SimpleTimer();
st.display();
}
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式