跪求带秒表功能的时钟代码,急! 问一下楼主,要不要图形界面?旨在dos窗口下以数字的形势显示可以不? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package info.bioz.test;import javax.swing.*;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.*;import java.util.Date;import java.text.SimpleDateFormat;/** * <p>File: StopWatch.java</p> * <p>Description: </p> * <p><a href="http://www.bioz.info/">BIOZ.info</a> Copyright (c) 2004</p> * * @author <a href="mailto:[email protected]">Chance</a> */public class StopWatch extends JFrame { JButton btnStart,btnStop; JLabel label; Timer timer; public StopWatch() { label=new JLabel("00:00:00.000"); btnStart=new JButton("start"); btnStop=new JButton("stop"); final int delay=100; final Date startTime=new Date(); final SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss.S"); final Action taskPerformer = new AbstractAction() { public void actionPerformed(ActionEvent evt) { //显示时间 Date d=new Date(System.currentTimeMillis()-startTime.getTime()-28800000); label.setText(sdf.format(d)); label.repaint(); } }; btnStart.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { startTime.setTime(new Date().getTime()); timer=new Timer(delay, taskPerformer); timer.start(); } }); btnStop.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { if(timer!=null&&timer.isRunning()) timer.stop(); } }); Container c=getContentPane(); c.add(label,BorderLayout.NORTH); c.add(btnStart,BorderLayout.CENTER); c.add(btnStop,BorderLayout.SOUTH); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { StopWatch window=new StopWatch(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.pack(); window.setVisible(true); }} J2SDK文件夹中的DEMO文件夹中有example 关于正则表达式的问题 用浏览器执行applet程序!! 正则 双引号转义匹配问题-牛人们有啥办法么? 急!急! 请问Java如何设置header让服务器发送普通页面而不是gzip压缩过的页面? 值的传递 两个组件实现事件的同步问题 请教applet问题! 配置连接池问题 关于Collections的copy集合运行时报错,下标越界,求解 stream.toArray(String[]::new)怎么理解合法性 如何控制Java的命令行方式屏蔽显,达到同一行刷新的效果? 帮帮我吧!在线等,及时接分的~.....
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;/**
* <p>File: StopWatch.java</p>
* <p>Description: </p>
* <p><a href="http://www.bioz.info/">BIOZ.info</a> Copyright (c) 2004</p>
*
* @author <a href="mailto:[email protected]">Chance</a>
*/
public class StopWatch extends JFrame {
JButton btnStart,btnStop;
JLabel label;
Timer timer;
public StopWatch() {
label=new JLabel("00:00:00.000");
btnStart=new JButton("start");
btnStop=new JButton("stop");
final int delay=100;
final Date startTime=new Date();
final SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss.S");
final Action taskPerformer = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
//显示时间
Date d=new Date(System.currentTimeMillis()-startTime.getTime()-28800000);
label.setText(sdf.format(d));
label.repaint();
}
};
btnStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
startTime.setTime(new Date().getTime());
timer=new Timer(delay, taskPerformer);
timer.start();
}
});
btnStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(timer!=null&&timer.isRunning())
timer.stop();
}
}); Container c=getContentPane();
c.add(label,BorderLayout.NORTH);
c.add(btnStart,BorderLayout.CENTER);
c.add(btnStop,BorderLayout.SOUTH);
}
public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
}); } private static void createAndShowGUI() {
StopWatch window=new StopWatch();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
}