重新修改了更新时间标签和更新时间的方法,使用Timer实例更新时间,并在更新时间的方法中增加了尝试重连的次数。
界面类:
/* * @(#)MainUI.java 0.1 2009-8-7 * Copyright 2006 DiaoxianSoft Development Team. All rights reserved. * DiaoxianSoft PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Timer; import java.util.TimerTask; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import easyJFC.*; import easyJFC.myUtil.*; /** * {Enter Class Description} * Copyright: Copyright (c) * Company: DiaoxianSoft development team * @author Geek_Soledad * @creation date 2009-8-7 下午10:10:40 * @version 0.1 */ public class MainUI extends JFrame { private static final long serialVersionUID = 1L; private TimeSynchro timeSyn = new TimeSynchro(); private final JTextField tfURL = new JTextField(); private final JLabel lbTime = new JLabel("null"); private final JButton btOK = new JButton("确定"); private final JButton btSynchro = new JButton("同步"); MainUI() { super("小Y时间同步 V0.1.3"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(400, 120); this.setResizable(false); this.setLocationRelativeTo(null); setIcon(); } // 设置窗口图标 private final void setIcon() { ImageIcon iconImg = null; iconImg = ImageUtil.getImageIconInJar(this, "res/icon.gif"); if (iconImg != null) { this.setIconImage(iconImg.getImage()); } else { this.setIconImage(new ImageIcon("res/icon.gif").getImage()); } } // 创建界面 public final void createUI() { this.setLayout(null); JLabel lbURL = new JLabel("当前同步的网址:"); tfURL.setText(timeSyn.getUrl().toString()); lbURL.setBounds(20, 15, 100, 25); tfURL.setBounds(125, 15, 175, 25); btOK.setBounds(310, 15, 60, 25); add(lbURL); add(tfURL); add(btOK); // 确定按钮的触发事件 btOK.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String url = tfURL.getText(); timeSyn.setUrl(url); } }); JLabel lbRightnow = new JLabel("该网站时间:"); lbRightnow.setBounds(20, 55, 70, 25); lbTime.setBounds(95, 55, 205, 25); btSynchro.setBounds(310, 55, 60, 25); add(lbRightnow); add(lbTime); // 同步时间按钮 add(btSynchro); btSynchro.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { timeSyn.setTime(); } }); if (System.getProperty("os.name").startsWith("Windows")) { JFrameOption.setLookAndFeelAsWindows(this); } setVisible(true); } /** * 更新标签时间 */ public void refreshLbTime() { Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { timeSyn.refreshTime(); lbTime.setText(timeSyn.getDate().toString()); System.out.println(timeSyn.getDate()); } }, 0, 1000); } public static void main(String[] args) { MainUI timeSynchro = new MainUI(); timeSynchro.createUI(); timeSynchro.refreshLbTime(); } }
实现功能类:
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Calendar; import java.util.Date; import javax.swing.JOptionPane; /* * @(#)TimeSynchro.java 0.1 2009-8-7 * Copyright 2006 DiaoxianSoft Development Team. All rights reserved. * DiaoxianSoft PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ /** * 6月5日下午,CMOS电池被拆下来重装,电脑时间回到从前。所以写了这个时间同步的小程序。 * Copyright: Copyright (c) * Company: DiaoxianSoft development team * @author Geek_Soledad * @creation date 2009-8-7 上午01:02:11 * @version 0.1.1 */ public class TimeSynchro { private byte retry = 3; // 连接失败时的重试次数 private URLConnection urlCon; private URL url; private Date date; // 获取到的日期 private Calendar cal = Calendar.getInstance(); public TimeSynchro() { try { url = new URL("http://www.bjtime.cn"); } catch (MalformedURLException e) { e.printStackTrace(); } refreshTime(); } public final URL getUrl() { return url; } public final void setUrl(URL url) { this.url = url; } public final void setUrl(String url) { System.out.println("setUrl"); try { this.url = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); try { this.url = new URL("http://www.bjtime.cn"); } catch (MalformedURLException e1) { e1.printStackTrace(); } } } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } /** * 更新时间 */ public final void refreshTime() { try { urlCon = url.openConnection(); urlCon.setConnectTimeout(1000); urlCon.connect(); System.out.print("refresh.."); } catch (IOException e) { if (retry > 0) { retry--; System.out.println("Trying to retry..."); } else { e.printStackTrace(); JOptionPane.showMessageDialog(null, "连接超时,请重新输入网址"); retry = 3; } } date = new Date(urlCon.getDate()); cal.setTime(date); } /** * 调用命令行设置时间 */ public final void setTime() { refreshTime(); try { // 判断当前操作系统并设定时间 if (System.getProperty("os.name").startsWith("Windows")) { java.lang.Runtime.getRuntime().exec( String.format("cmd /c date %1$tY/%1$tm/%1$td", cal)); java.lang.Runtime.getRuntime().exec( String.format("cmd /c time %1$tH:%1$tM:%1$tS", cal)); } else if(System.getProperties().equals("Linux")){ java.lang.Runtime.getRuntime().exec( String.format("date %1$tm%1$td%1$tH%1$tM%1$tY.%1$tS", cal)); } else { System.out.println(System.getProperty("os.name")); } } catch(java.io.IOException e) { e.printStackTrace(); } } }