java秒表的代码,java 秒
求一个java秒表代码
package info.bioz.test;
十载的蕲春网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整蕲春建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“蕲春网站设计”,“蕲春网站推广”以来,每个客户项目都认真落实执行。
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
* pFile: StopWatch.java/p
* pDescription: /p
* pa href=""BIOZ.info/a Copyright (c) 2004/p
*
* @author a href="mailto:chancewang78@hotmail.com"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!=nulltimer.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 秒表
package demo;
import javax.swing.*;
import java.awt.HeadlessException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Timer extends JFrame {
private static final long serialVersionUID = 1L;
private static final String INITIAL_LABEL_TEXT = "00:00:00 000";
// 计数线程
private CountingThread thread = new CountingThread();
// 记录程序开始时间
private long programStart = System.currentTimeMillis();
// 程序一开始就是暂停的
private long pauseStart = programStart;
// 程序暂停的总时间
private long pauseCount = 0;
private JLabel label = new JLabel(INITIAL_LABEL_TEXT);
private JButton startPauseButton = new JButton("开始");
private JButton resetButton = new JButton("清零");
private ActionListener startPauseButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (thread.stopped) {
pauseCount += (System.currentTimeMillis() - pauseStart);
thread.stopped = false;
startPauseButton.setText("暂停");
} else {
pauseStart = System.currentTimeMillis();
thread.stopped = true;
startPauseButton.setText("继续");
}
}
};
private ActionListener resetButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
pauseStart = programStart;
pauseCount = 0;
thread.stopped = true;
label.setText(INITIAL_LABEL_TEXT);
startPauseButton.setText("开始");
}
};
public Timer(String title) throws HeadlessException {
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(300, 300);
setResizable(false);
setupBorder();
setupLabel();
setupButtonsPanel();
startPauseButton.addActionListener(startPauseButtonListener);
resetButton.addActionListener(resetButtonListener);
thread.start(); // 计数线程一直就运行着
}
// 为窗体面板添加边框
private void setupBorder() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.setContentPane(contentPane);
}
// 配置按钮
private void setupButtonsPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(startPauseButton);
panel.add(resetButton);
add(panel, BorderLayout.SOUTH);
}
// 配置标签
private void setupLabel() {
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40));
this.add(label, BorderLayout.CENTER);
}
// 程序入口
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
Timer frame = new Timer("计时器");
frame.pack();
frame.setVisible(true);
}
private class CountingThread extends Thread {
public boolean stopped = true;
private CountingThread() {
setDaemon(true);
}
@Override
public void run() {
while (true) {
if (!stopped) {
long elapsed = System.currentTimeMillis() - programStart - pauseCount;
label.setText(format(elapsed));
}
try {
sleep(1); // 1毫秒更新一次显示
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
}
// 将毫秒数格式化
private String format(long elapsed) {
int hour, minute, second, milli;
milli = (int) (elapsed % 1000);
elapsed = elapsed / 1000;
second = (int) (elapsed % 60);
elapsed = elapsed / 60;
minute = (int) (elapsed % 60);
elapsed = elapsed / 60;
hour = (int) (elapsed % 60);
return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli);
}
}
}
你可以试试,希望能帮到你!
Java做的秒表: 代码已有 求高人给中文注释(结构分析)
代码太长,怕吞了。。。
public class TestTimer extends JFrame implements ActionListener, Runnable {
private static TestTimer obj; // 自己的一个静态实例,在这里没什么特别的意思
private JButton btnStart; // 开始按钮
private JButton btnPause; // 暂停按钮
private JButton btnResume; // 恢复按钮
private JButton btnStop; // 停止按钮
private JLabel lblTime; // 显示时间的Label(中文是标签?)
private static Thread th; // 一个用来控制时间的线程
private long count; // 计数
public TestTimer(){
super("秒表"); // TestTimer继承JFrame,这里调用父类的构造方法,传入的参数表示窗口的标题
btnStart = new JButton("开始"); // 初始化按钮,传入的参数表示按钮上显示的文字
btnPause = new JButton("暂停"); // 同上
btnResume = new JButton("继续"); // 同上
btnStop = new JButton("停止"); // 同上
lblTime = new JLabel("00:00:00.000"); // 初始化Label,传入的参数表示Label上显示的文字
this.setLayout(new FlowLayout()); // 设置layout风格为FlowLayout(就是设置控件的摆放方式)
this.add(btnStart); // 将控件加入到窗口中
this.add(btnPause); // 同上
this.add(btnResume); // 同上
this.add(btnStop); // 同上
this.add(lblTime); // 同上
btnStart.addActionListener(this); // 为按钮添加监听器(为什么是this,因为TestTimer类实现了ActionListener接口,所以可以这样用)
btnPause.addActionListener(this); // 为按钮添加监听器(但我不建议这样,这样的话类的职责不明确)
btnResume.addActionListener(this); // 为按钮添加监听器(当然,如果只是实现需求,怕麻烦可以这么做)
btnStop.addActionListener(this); // 为按钮添加监听器
this.setSize(150, 200); // 设置窗口大小
this.setVisible(true); // 显示窗口
}
public static void main(String[] args) {
obj = new TestTimer(); // 主函数入口,初始化实例(其实就是启动窗口)
}
public void actionPerformed(ActionEvent e) {// 这里是实现ActionListener接口的地方
JButton btn = (JButton)e.getSource(); // 获得是哪个按钮触发了事件
if(btn.getText().equals("开始")){ // 如果是开始按钮
th = new Thread(obj); // 初始化一个线程(传入obj是因为,TestTimer类实现了Runnable接口,同样我不建议这样做)
count = 0; // count计数器清零
th.start(); // 线程启动
}
else if(btn.getText().equals("暂停")){ // 如果是暂停按钮
th.suspend(); // 线程挂起(这个方法已经被新版本的JDK遗弃,你可以用,但不推荐用)
}
else if(btn.getText().equals("继续")){ // 如果是继续按钮
th.resume(); // 线程恢复(同上)
}
else if(btn.getText().equals("停止")){ // 如果是停止按钮
th.stop(); // 线程停止(同上)
}
}
@Override
public void run() { // 实现Runnable接口的地方
while(true){ // 无限循环(线程一直运行着记录时间)
int ms, seconds, minutes, hours; // 下面一整段都是根据count这个计数器来计算时间
// 你看到最后有一个Thread.sleep(1)表示该线程每毫秒工作一次,起到计数的作用)
String msg = ""; // msg表示Label上显示的时间
hours = (int)(count / 3600000);
minutes = (int)((count - hours * 3600000) / 60000);
seconds = (int)((count - hours * 3600000 - minutes * 60000) / 1000);
ms = (int)(count % 1000);
if(hours 10){ // 下面这一串是用来做msg的格式
msg += "0" + hours + ":";
}
else{
msg += hours + ":";
}
if(minutes 10){
msg += "0" + minutes + ":";
}
else{
msg += minutes + ":";
}
if(seconds 10){
msg += "0" + seconds + ":";
}
else{
msg += seconds + ":";
}
if(ms 10){
msg += "00" + ms;
}
else if(ms 100){
msg += "0" + ms;
}
else{
msg += ms;
}
lblTime.setText(msg); // 显示时间到Label上
count++; // 计数器递增
try {
Thread.sleep(1); // 线程挂起1毫秒(也即,线程每毫秒执行一次循环)
}
catch (InterruptedException e) { // 异常处理(不必管,必须这样写)
e.printStackTrace();
}
}
}
}
JAVA需要一条秒表计时器代码
好吧,已看到你的评论,我在这里再回答一次:
1)你所说的置顶如果是属于悬浮窗效果,那么JFrame实例化后,再添加一行如下的代码:
form1.setAlwaysOnTop(true);// 总是允许窗口置顶
2)时分秒更简单了,除一除转转换就行了,没有技术含量。
3)快捷键通过JButton类的setMnemonic方法实现
So,综上,整个程序的实现算法如下:
package hky.example;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.plaf.OptionPaneUI;
public class Demo{
static boolean isRuning=false;
static boolean isFirst=true;
static Integer hmsCounter=0;
static int hour,minute,second;
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
JFrame form1 = new JFrame("Form1");
form1.setAlwaysOnTop(true);// 1)总是允许窗口置顶
JTextField jTextField = new JTextField(10);
jTextField.setSize(10, 10);
jTextField.setText("0");
jTextField.setEditable(false);
JButton jButton = new JButton("开始");
jButton.setSize(10, 10);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
while(isRuning){
++hmsCounter;
//3)时分秒显示
hour=hmsCounter/3600;
minute=hmsCounter%3600/60;
second=hmsCounter%60;
jTextField.setText(hour+"时"+minute+"分"+second+"秒");
try {Thread.sleep(1000);} catch (Exception e2) {}
}
try {Thread.sleep(200);} catch (Exception e2) {}// 修复上一次回答的版本可能会存在的Bug
}
}
});
jButton.setMnemonic(KeyEvent.VK_ENTER);// 2)给JButton发送 Alt+Enter快捷键
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text=jButton.getText().equals("开始")?"暂停":"开始";
jButton.setText(text);
isRuning=!isRuning;
if(isFirst){
thread.start();
isFirst=false;
}
}
});
JPanel panel = new JPanel();
panel.setSize(200, 200);
panel.add(jTextField, BorderLayout.NORTH);
panel.add(jButton, BorderLayout.CENTER);
form1.add(panel);
form1.setBounds(200, 100, 250, 150);
form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form1.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// 窗口关闭前取出文本框的数字保存到外部文件,代码在此处写
JOptionPane.showMessageDialog(null, "Are you sure closing?");
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
form1.setVisible(true);
}
}
本文标题:java秒表的代码,java 秒
文章转载:http://scyanting.com/article/hoshec.html