Java做的秒表: 代码已有 求高人给中文注释(结构分析)代码太长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遗弃,java秒表的源代码你可以用 , 但不推荐用)
}
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这个计数器来计算时间
// java秒表的源代码你看到最后有一个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(hours10){// 下面这一串是用来做msg的格式
msg= "0"hours":";
}
else{
msg= hours":";
}
if(minutes10){
msg= "0"minutes":";
}
else{
msg= minutes":";
}
if(seconds10){
msg= "0"seconds":";
}
else{
msg= seconds":";
}
if(ms10){
msg= "00"ms;
}
else if(ms100){
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=hmsCounter600/60;
second=hmsCounter`;
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的计时器代码,应该比较简单的 , 来看看吧 。package test;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test5 extends Applet {
private final Panel pan = new Panel();
private final Label time = new Label();
private final Button btnGo = new Button("开始");
private final Button btnPouse = new Button("暂停");
private final Button btnReset = new Button("复位");
private final StopwatchThread swThread = new StopwatchThread();
private class btnGoListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.go();
btnGo.setEnabled(false);
}
}
private class btnPouseListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(btnGo.isEnabled()){
return ;
【java秒表的源代码 java秒表程序设计】}
if (btnPouse.getLabel().equals("继续")) {
swThread.go();
btnPouse.setLabel("暂停");
} else if (btnPouse.getLabel().equals("暂停")) {
swThread.noGo();
btnPouse.setLabel("继续");
}
}
}
private class btnResetListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
swThread.reset();
btnGo.setEnabled(true);
btnGo.setLabel("开始");
btnPouse.setLabel("暂停");
}
}
private class StopwatchThread extends Thread {
private boolean going = false;
private long prevElapsed = 0;
private Date startDate = new Date();
private long elapsedTime() {
return prevElapsed
(going ? new Date().getTime() - startDate.getTime() : 0);
}
private String msToString(long time) {
System.out.println(time "" ((0*60 2)*1000 999));
if(((99*60 59)*1000 983)=time((99*60 59)*1000 999)=time){//((0*60 2)*1000 983)=time((0*60 2)*1000 999)=time
if (time % 1000990)
time= 2;
swThread.noGo();
}
String ms, sec, min;
if (time % 10 = 5)
time= 5;
ms = Long.toString(time % 1000);
while (ms.length()3)
ms = "0"ms;
ms = ms.substring(0, ms.length() - 1);
time /= 1000;
sec = Long.toString(time % 60);
if (sec.length() == 1) sec = "0"sec;
time /= 60;
min = Long.toString(time);
return min":"sec"."ms;
}
public void go() {
startDate = new Date();
going = true;
}
public void noGo() {
prevElapsed = elapsedTime();
going = false;
}
public void reset() {
going = false;
prevElapsed = 0;
}
public void run() {
while (true) {
time.setText(msToString(elapsedTime()));
yield();
}
}
}
public void init() {
setLayout(new GridLayout(2,2));
setBackground(Color.lightGray);
setForeground(Color.black);
pan.setLayout(new GridLayout(3,2));
pan.add(new Label("计时:"));
time.setForeground(Color.blue);
pan.add(time);
pan.add(btnGo);
pan.add(btnPouse);
pan.add(btnReset);
pan.add(new Label());
add(pan);
btnGo.addActionListener(new btnGoListener());
btnReset.addActionListener(new btnResetListener());
btnPouse.addActionListener(new btnPouseListener());
swThread.setDaemon(true);
swThread.start();
}
public static void main(String[] args) {
Test5 applet = new Test5();
Frame aFrame = new Frame("计时器");
aFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(400, 200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
可以改变有注释的那个if语句里面的值来判断什么时候停止
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("d:d:d d", hour, minute, second, milli);
}
}
}
你可以试试,希望能帮到你!
java秒表的源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java秒表程序设计、java秒表的源代码的信息别忘了在本站进行查找喔 。
推荐阅读
- 制造业erp系统应用分析,制造业erp系统软件有哪些
- asp.net获取电子秤,aspnet获取客户端计算机名
- IE浏览器局域网代理设置,ie浏览器怎么设置代理服务
- python算平均分函数 python求平均分的代码
- 怎么看显卡不支持dx11,怎么看显卡支不支持dx11
- 怎么在word里输入方框,word 如何输入方框
- 荆州专注sap维护服务,荆州专注sap维护服务中心
- oracle如何插表 oracle往表里添加数据
- 公众号文章比例怎么调,公众号文章怎么转载