Java秒表优化版(同步系统时间)(线程)(记录)
来源:互联网 发布:江苏省邮币卡软件下载 编辑:程序博客网 时间:2024/05/29 07:58
按钮换成了圆形,看起来高大上!!!哈哈哈package Seconds;public class TestMain {public static void main(String[] args) {Watch_time w=new Watch_time();Watch frame = new Watch(w);w.set(frame); }}
package Seconds;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class Watch_time extends Thread{ Watch w; long l,l1,l2,l3;//l 表示 获取的当前系统时间 l1表示暂停的时间 l2表示 暂停后开始的时间 l3 为暂停的时间长短 Calendar c; SimpleDateFormat sf=new SimpleDateFormat("mm:ss.SSS"); public void set(Watch w) { this.w=w; } public void setEmpty() { // 重置 l1=l2=l3=0; }public void setStart() { // 获取系统当前时间 c=Calendar.getInstance();l =c.getTimeInMillis();}public void run() { while(true) {try {Thread.sleep(1);String s=sf.format(new Date().getTime()-l-l3); w.textArea.setText(s);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
package Seconds;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JTextArea;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Alignment;import javax.swing.JButton;import java.awt.event.ActionListener;import java.util.Date;import java.awt.event.ActionEvent;import javax.swing.JScrollPane;import java.awt.SystemColor;import java.awt.Font;import javax.swing.UIManager;import javax.swing.LayoutStyle.ComponentPlacement;import org.eclipse.wb.swing.FocusTraversalOnArray;import java.awt.Component;import java.awt.Color;public class Watch extends JFrame {/** * */private static final long serialVersionUID = 1L;private JPanel contentPane; JTextArea textArea = new JTextArea(); boolean a=true; //判断是 是否为 第一次 开始; int i=0; boolean b=false; // 判断是否为 重置的状态 boolean c=false; // 判断是否为 暂停 后的 开始 状态protected Object lock;public Watch(Watch_time w) {setTitle("\u79D2\u8868\u8BA1\u65F6\u5668 --\u00A9Dong");textArea.setText("00:00.00");setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);contentPane = new JPanel();JScrollPane scrollPane = new JScrollPane();scrollPane.setEnabled(false);contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);JTextArea textArea_1 = new JTextArea();textArea_1.setFont(new Font("Monospaced", Font.BOLD | Font.ITALIC, 13));textArea_1.setEditable(false);textArea_1.setBackground(UIManager.getColor("Button.background"));JTextArea textArea_2 = new JTextArea();textArea_2.setFont(UIManager.getFont("Viewport.font"));textArea_2.setEditable(false);textArea_2.setText(" \u51C6\u5907\u8BA1\u65F6");textArea_2.setBackground(SystemColor.control);JButton btnNewButton=new CircleButton("开始",Color.GREEN);btnNewButton.setBackground(Color.CYAN);JButton btnNewButton_1=new CircleButton("\u8BB0\u5F55",Color.GREEN);JButton btnNewButton_2=new CircleButton("\u91CD\u7F6E",Color.GREEN);//开始或者暂停btnNewButton.addActionListener(new ActionListener() {@SuppressWarnings("deprecation")public void actionPerformed(ActionEvent e) {if(btnNewButton.getText().equals("开始")){ if(a) {w.setStart();w.start();textArea_2.setText(" 正在计时"); btnNewButton_1.setEnabled(true); btnNewButton_2.setEnabled(false);btnNewButton.setText("暂停");a=false;}else {w.l2=new Date().getTime();if(c){w.l2=0;c=false;}if(b) {w.setStart();b=false;}w.l3=w.l2-w.l1+w.l3;w.resume();textArea_2.setText(" 正在计时");btnNewButton_1.setEnabled(true);btnNewButton_2.setEnabled(false); btnNewButton.setText("暂停");}}else {w.l1=new Date().getTime();w.suspend();textArea_2.setText(" 暂 "+" 停");btnNewButton_1.setEnabled(false);btnNewButton_2.setEnabled(true);btnNewButton.setText("开始");}}});//计数btnNewButton_1.setEnabled(false);btnNewButton_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {textArea_1.append("第"+(++i)+"次计数:"+textArea.getText()+'\n');}});//重置btnNewButton_2.setEnabled(false);btnNewButton_2.addActionListener(new ActionListener() {@SuppressWarnings("deprecation")public void actionPerformed(ActionEvent e) {textArea_1.setText("");i=0;btnNewButton_2.setEnabled(false);textArea.setText("00:00.00");textArea_2.setText(" 准备计时");w.setEmpty();w.suspend(); b=true; c=true;}});//布局GroupLayout gl_contentPane = new GroupLayout(contentPane);gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE).addGroup(gl_contentPane.createSequentialGroup().addGap(48).addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE).addGap(38).addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane.createSequentialGroup().addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE).addGap(44).addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE).addContainerGap()).addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup().addComponent(textArea, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE).addContainerGap(134, Short.MAX_VALUE)).addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup().addComponent(textArea_2, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE).addGap(178)))));gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addGroup(gl_contentPane.createSequentialGroup().addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE).addGap(33).addComponent(textArea, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addPreferredGap(ComponentPlacement.RELATED).addComponent(textArea_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addGap(15).addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE).addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE).addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)).addGap(44)));textArea.setEditable(false);textArea.setFont(new Font("Monospaced", Font.BOLD | Font.ITALIC, 16));textArea.setBackground(SystemColor.control);scrollPane.setViewportView(textArea_1);contentPane.setLayout(gl_contentPane);contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{scrollPane, btnNewButton, textArea_1, btnNewButton_1, textArea, btnNewButton_2, textArea_2}));}}
阅读全文
0 0
- Java秒表优化版(同步系统时间)(线程)(记录)
- java(线程)实现一个秒表功能
- 简单的Java秒表计时器(线程)
- Java计时器(秒表)
- 【Java】Java计时器(秒表)
- java线程(线程同步)
- java线程同步(生产者消费者应用-模拟叫号系统)
- 秒表(计时器)
- Java 线程同步(synchronized)
- JAVA线程同步(一)
- JAVA线程同步(二)
- Java线程同步(一)
- Java线程同步(二)
- Java线程同步(三)
- java基础---Java---面试题---银行业务调度系统(线程同步锁、枚举、线程池)
- java基础---Java---面试题---银行业务调度系统(线程同步锁、枚举、线程池)
- java线程(二)线程同步问题
- Java线程总结(2):线程同步
- 单例模式(懒汉式和饿汉式)
- ChannelOption属性说明
- ES6 标签模板与模板字符串
- Spring Boot配置文件简单介绍
- SVN的学习之路五(权限分配)
- Java秒表优化版(同步系统时间)(线程)(记录)
- Java Web实现定时任务quartz方式
- 1133. Splitting A Linked List (25)
- 成为一名推荐系统工程师永远都不晚
- bootstrap输入框及验证
- springboot启动的三种方式
- DockerFile
- 如何在调用方法时,让方法中的参数指向元素本身
- 谁先倒(15 分) 划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和,谁就输了,输家罚一杯酒