Java秒表优化版(同步系统时间)(线程)(记录)

 
  
按钮换成了圆形,看起来高大上!!!哈哈哈
 
 
 
  
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}));}}
 
  


 

你可能感兴趣的:(java小程序)