怎么用JAVA 实现秒表功能 给我一点思路! 谢谢 怎么用JAVA 实现秒表功能 给我一点思路! 由于是初学者,所以不是太会的。希望大家帮我下 给我开拓下思路谢谢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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); } } 上面一个没排版 import javax.swing.*;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.*;import java.util.Date;import java.text.SimpleDateFormat;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); }} 有点难啊 我那个java.lang.…………什么的 都没学会那有点看不懂 不过谢谢大家 帮我编写他了。 问了很多人都不知道的问题,100分求助下!关于重构里面的apply scripts [求助]关于System.out.flush(); java反射机制 timer在java中怎么使用的 JTable 设置选择的范围? 怎么实现"浏览"那样的按钮的功能 小妹有个问题,希望高手帮忙!!! 关于HashMap()里的values()方法的一个深深的问题! 请教一个简单的问题,关于package的问题 Java能否和Excel进行交互? 点击按钮改变背景颜色 java中Api文档怎么用啊??
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);
}
}
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;
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);
}
}