日期:2014-05-20 浏览次数:20813 次
import java.awt.*; import java.awt.event.*; import java.text.SimpleDateFormat; import java.util.*; import javax.swing.*; import javax.swing.border.*; public class TestTimePieceJFrame extends JFrame { ControlPanel controlPane = null; //-----------------控制面板 JTextField jTextField; public TestTimePieceJFrame() { super(); init(); } private void init() { controlPane = new ControlPanel(this); add(controlPane); setPreferredSize(new Dimension(300, 170)); pack(); setVisible(true); setResizable(false); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); addKeyListener(new TimePieceKeyListener()); } public static void main(String[] args) { new TestTimePieceJFrame(); ; } class TimePieceKeyListener extends KeyAdapter { public void keyReleased(KeyEvent e) { controlPane.keyReleased(e); } } } class ControlPanel extends JPanel { private JFrame snakeFrame = null; JLabel jLabel,jLabel1, jLabel2, jLabel3, jLabel4; Date dateWhenStartJpanel;//保存程序启动时的时间 Date dateWhenClickF5; Thread thread ; public ControlPanel(JFrame snakeFrame) { super(); this.snakeFrame = snakeFrame; init(); } private void init() { dateWhenStartJpanel = new Date(); setSize(608, 31); setBackground(Color.WHITE); setLayout(new FlowLayout()); setBorder(new LineBorder(Color.white, 5)); setLayout(new FlowLayout()); jLabel = new JLabel(); jLabel1 = new JLabel(); jLabel2 = new JLabel(); jLabel3 = new JLabel(); jLabel4 = new JLabel(); add(jLabel); add(jLabel1); add(jLabel2); add(jLabel3); add(jLabel4); jLabel.setText("F5:开始计时 F6:停止计时"); } public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_F5) { dateWhenClickF5 = new Date();//sava time when click F5 thread = new Thread(new TimePieceRunnable()) ;thread.start(); } else if (keyCode == KeyEvent.VK_F6) { if(thread != null)thread.stop(); else prompt("请先按F5"); } } class TimePieceRunnable implements Runnable { public void run() { while (true) { Date tempDate = new Date(); String tempString = formateToTime(tempDate, "现在时间是 E kk:mm:ss:"); jLabel1.setText(tempString); tempString = formateToTime(dateWhenStartJpanel, "启动程序时间是 E kk:mm:ss:"); jLabel2.setText(tempString); tempString = formateToTime(dateWhenClickF5, "最近一次按动F5时间是 E kk:mm:ss:"); jLabel3.setText(tempString); tempDate = getMillisOfTimeDifference(dateWhenClickF5, tempDate); tempString = formateToTime(tempDate, "最近一次按动F5到现在的时间差是 kk:mm:ss:"); jLabel4.setText(tempString); try { repaint(); Thread.sleep(100);////每隔0.1秒刷新一次 } catch (InterruptedException e) { e.printStackTrace(); } } } } public String formateToTime(Date date, String timeRegex) { //Date date = new Date(); //SimpleDateFormat f = new SimpleDateFormat("'BeiJing Time':yyyy年MM月dd日 E kk时mm分ss秒"); SimpleDateFormat f = new SimpleDateFormat(timeRegex); String newTypeDate = f.format(date); return newTypeDate; } public Date getMillisOfTimeDifference(Date date1, Date date2) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date1); long timelnMillis1 = calendar.getTimeInMillis(); calendar.setTime(date2); long timelnMillis2 = calendar.getTimeInMillis(); calendar.setTimeInMillis(timelnMillis2 - timelnMillis1); return calendar.getTime(); } private static int prompt(String promptMessage) { return JOptionPane.showConfirmDialog(null, promptMessage, "友情提示", JOptionPane.WARNING_MESSAGE); } }