diff --git a/src/com/casic/swing/ui/TimeGuardNtp.form b/src/com/casic/swing/ui/TimeGuardNtp.form index b2d5ff2..31eb394 100644 --- a/src/com/casic/swing/ui/TimeGuardNtp.form +++ b/src/com/casic/swing/ui/TimeGuardNtp.form @@ -5,7 +5,9 @@ - + + + diff --git a/src/com/casic/swing/ui/TimeGuardNtp.form b/src/com/casic/swing/ui/TimeGuardNtp.form index b2d5ff2..31eb394 100644 --- a/src/com/casic/swing/ui/TimeGuardNtp.form +++ b/src/com/casic/swing/ui/TimeGuardNtp.form @@ -5,7 +5,9 @@ - + + + diff --git a/src/com/casic/swing/ui/TimeGuardNtp.java b/src/com/casic/swing/ui/TimeGuardNtp.java index eadd4b0..1be9340 100644 --- a/src/com/casic/swing/ui/TimeGuardNtp.java +++ b/src/com/casic/swing/ui/TimeGuardNtp.java @@ -6,7 +6,6 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder; import javax.swing.*; -import javax.swing.border.EmptyBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.*; @@ -21,7 +20,7 @@ /** * @author a203 */ -public class TimeGuardNtp { +public class TimeGuardNtp extends JFrame { private JPanel ntpPanel; private JComboBox hostComboBox; private JLabel timeValueLabel; @@ -32,72 +31,73 @@ private JComboBox periodComboBox; private JPanel stateView; - private static final String[] HOST_NAME = {"NTP服务器(上海)", "中国国家授时中心", "清华大学", "北京大学", "自定义"}; - private static final String[] HOST_IP = {"ntp.api.bz", "ntp.ntsc.ac.cn", "s1b.time.edu.cn", "s2m.time.edu.cn"}; + private final String[] HOST_NAME = {"NTP服务器(上海)", "中国国家授时中心", "清华大学", "北京大学", "自定义"}; + private final String[] HOST_IP = {"ntp.api.bz", "ntp.ntsc.ac.cn", "s1b.time.edu.cn", "s2m.time.edu.cn"}; /** * 同步周期,单位:小时 */ - private static final Integer[] PERIOD = {1, 6, 12, 24}; + private final Integer[] PERIOD = {1, 6, 12, 24}; private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor( 1, new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build()); private boolean hasNtp = false; private String host = HOST_IP[0]; + private int period = 1; public static void main(String[] args) { - TimeGuardNtp timeGuard = new TimeGuardNtp(); - JFrame frame = new JFrame("TimeGuardNtp"); - frame.setContentPane(timeGuard.ntpPanel); - frame.setResizable(false); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.pack(); - frame.setSize(410, 280); + new TimeGuardNtp(); + } + + public TimeGuardNtp() { + setContentPane(ntpPanel); + setResizable(false); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + pack(); + setSize(410, 280); //居中 - frame.setLocationRelativeTo(null); - frame.setVisible(true); - //内边距 - timeGuard.ntpPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); - timeGuard.dotView.setText("未同步"); - setStateView(timeGuard, Color.GRAY); + setLocationRelativeTo(null); + setVisible(true); + dotView.setText("未同步"); + setStateView(Color.GRAY); StringHelper.createLogFile(); /** * 时间间隔,单位为毫秒 * */ - timeGuard.currentTimeLabel.setForeground(Color.BLUE); + currentTimeLabel.setForeground(Color.BLUE); new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String systemTime = TimeOrDateUtil.timestampToTime(System.currentTimeMillis()); - timeGuard.currentTimeLabel.setText(systemTime); + currentTimeLabel.setText(systemTime); } }).start(); String assertsData = StringHelper.getAssertsData(); if (!"".equals(assertsData)) { - timeGuard.timeValueLabel.setText(assertsData); + timeValueLabel.setText(assertsData); } else { - timeGuard.timeValueLabel.setText("无法确定最近同步时间"); + timeValueLabel.setText("无法确定最近同步时间"); } //初始化JComboBox - initComBox(timeGuard); - timeGuard.hostComboBox.addItemListener(new ItemListener() { + initComBox(); + hostComboBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { String item = (String) e.getItem(); if (e.getStateChange() == ItemEvent.SELECTED) { if ("自定义".equals(item)) { - String inputContent = JOptionPane.showInputDialog(timeGuard.ntpPanel, "请输入授时中心域名或者IP地址", "", JOptionPane.INFORMATION_MESSAGE); + String inputContent = JOptionPane.showInputDialog(ntpPanel, "请输入授时中心域名或者IP地址", "", JOptionPane.INFORMATION_MESSAGE); if (inputContent.isEmpty()) { - JOptionPane.showMessageDialog(timeGuard.ntpPanel, "输入错误,请检查", "Runtime Error", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(ntpPanel, "输入错误,请检查", "Runtime Error", JOptionPane.ERROR_MESSAGE); } else { - timeGuard.host = inputContent; + host = inputContent; } } else { for (int i = 0; i < HOST_NAME.length; i++) { if (item.equals(HOST_NAME[i])) { - timeGuard.host = HOST_IP[i]; + host = HOST_IP[i]; } } } @@ -110,34 +110,34 @@ @Override protected Boolean doInBackground() { - timeGuard.hasNtp = CommandUtil.checkEnv(); - return timeGuard.hasNtp; + hasNtp = CommandUtil.checkEnv(); + return hasNtp; } @Override protected void done() { - if (!timeGuard.hasNtp) { - JOptionPane.showMessageDialog(timeGuard.ntpPanel, "未当前设备发现可用的NTP配置", "Runtime Error", JOptionPane.ERROR_MESSAGE); + if (!hasNtp) { + JOptionPane.showMessageDialog(ntpPanel, "未当前设备发现可用的NTP配置", "Runtime Error", JOptionPane.ERROR_MESSAGE); //环境不对,关闭窗体 - frame.dispose(); + dispose(); } super.done(); } }.execute(); - timeGuard.autoCheckBox.addChangeListener(new ChangeListener() { + autoCheckBox.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { JCheckBox checkBox = (JCheckBox) e.getSource(); - timeGuard.updateTimeButton.setEnabled(!checkBox.isSelected()); - timeGuard.periodComboBox.setEnabled(checkBox.isSelected()); + updateTimeButton.setEnabled(!checkBox.isSelected()); + periodComboBox.setEnabled(checkBox.isSelected()); } }); - timeGuard.periodComboBox.addItemListener(new ItemListener() { + periodComboBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { - int period = (Integer) e.getItem(); + period = (Integer) e.getItem(); if (e.getStateChange() == ItemEvent.SELECTED) { /** * 开启同步,只能开启一个同步线程 @@ -147,10 +147,10 @@ * 如果上一个任务执行完毕,则当前任务立即执行 * 如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行 * */ - timeGuard.executorService.scheduleAtFixedRate(new Runnable() { + executorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { - updateView(timeGuard); + updateView(); } }, 0, period, TimeUnit.HOURS); } @@ -158,43 +158,43 @@ }); //按钮点击事件 - timeGuard.updateTimeButton.addActionListener(new ActionListener() { + updateTimeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - updateView(timeGuard); + updateView(); } }); } - private static void updateView(TimeGuardNtp timeGuard) { - String result = CommandUtil.ntpDate(timeGuard.host); + private void updateView() { + String result = CommandUtil.ntpDate(host); System.out.println("命令执行结果 ===> " + result); boolean isSuccess = result.contains("step time server") || result.contains("adjust time server"); if (isSuccess) { String systemTime = TimeOrDateUtil.timestampToTime(System.currentTimeMillis()); - setStateView(timeGuard, Color.GREEN); - timeGuard.dotView.setText("同步成功"); - timeGuard.timeValueLabel.setText(systemTime); + setStateView(Color.GREEN); + dotView.setText("同步成功"); + timeValueLabel.setText(systemTime); //同步成功之后将时间存入本地 StringHelper.saveAssertsData(systemTime); } else { - setStateView(timeGuard, Color.RED); - timeGuard.dotView.setText("同步失败"); - timeGuard.timeValueLabel.setText(""); + setStateView(Color.RED); + dotView.setText("同步失败"); + timeValueLabel.setText(""); } } - private static void initComBox(TimeGuardNtp timeGuard) { + private void initComBox() { for (String s : HOST_NAME) { - timeGuard.hostComboBox.addItem(s); + hostComboBox.addItem(s); } for (Integer integer : PERIOD) { - timeGuard.periodComboBox.addItem(integer); + periodComboBox.addItem(integer); } } - private static void setStateView(TimeGuardNtp timeGuard, Color color) { - timeGuard.stateView.setPreferredSize(new Dimension(15, 15)); - timeGuard.stateView.setBackground(color); + private void setStateView(Color color) { + stateView.setPreferredSize(new Dimension(15, 15)); + stateView.setBackground(color); } }