小小的程序3之时间同步时间:2021-08-13 17:11:05UI类。 /* * @(#)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 javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;import easyJFC.JFrameOption;/** * {Enter Class Description} * Copyright: Copyright (c) * Company: DiaoxianSoft development team * @author Geek_Soledad * @creation date 2009-8-7 下午10:10:40 * @version 0.1 * * Modification History: *Date Author Version Description *2009-8-7 Geek_Soledad Initialize */public class MainUI extends JFrame implements Runnable { private static final long serialVersionUID = 1L; private TimeSynchro timeSyn = new TimeSynchro(); private boolean isShow = true; private boolean isRefresh = true; 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.2"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(400, 120); this.setResizable(false); this.setLocationRelativeTo(null); setIcon(); } // 设置窗口图标 private final void setIcon() { ImageIcon iconImg = null; iconImg = JFrameOption.getImageIcon(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); isRefresh = timeSyn.refreshTime(); } }); 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 static void main(String[] args) { MainUI timeSynchro = new MainUI(); timeSynchro.createUI(); Thread t = new Thread(timeSynchro); t.start(); } /* 该线程显示并刷新标签时间。 */ public void run() { while(isShow) { if ( isRefresh) { isRefresh = timeSyn.refreshTime(); lbTime.setText(timeSyn.getDate().toString()); // System.out.println(timeSyn.date); } try { Thread.sleep(700); } catch (InterruptedException e) { e.printStackTrace(); } } }} 功能实现: 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 * * Modification History: *Date Author Version Description *2009-8-7 Geek_Soledad 0.1 Initialize *2011-06-12 Geek_Soledad 0.1.1 Modify the method of judgment OS's type */public class TimeSynchro { 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 boolean refreshTime() { try { urlCon = url.openConnection(); urlCon.setConnectTimeout(3000); urlCon.connect(); } catch (IOException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "连接超时,请重新输入网址"); return false; } date = new Date(urlCon.getDate()); cal.setTime(date); return true; } /** * 调用命令行设置时间 */ 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(); } }}