diff --git a/image/dot_gray.png b/image/dot_gray.png deleted file mode 100644 index 57254db..0000000 --- a/image/dot_gray.png +++ /dev/null Binary files differ diff --git a/image/dot_gray.png b/image/dot_gray.png deleted file mode 100644 index 57254db..0000000 --- a/image/dot_gray.png +++ /dev/null Binary files differ diff --git a/image/dot_green.png b/image/dot_green.png deleted file mode 100644 index 05f439d..0000000 --- a/image/dot_green.png +++ /dev/null Binary files differ diff --git a/image/dot_gray.png b/image/dot_gray.png deleted file mode 100644 index 57254db..0000000 --- a/image/dot_gray.png +++ /dev/null Binary files differ diff --git a/image/dot_green.png b/image/dot_green.png deleted file mode 100644 index 05f439d..0000000 --- a/image/dot_green.png +++ /dev/null Binary files differ diff --git a/image/dot_red.png b/image/dot_red.png deleted file mode 100644 index bc64f7c..0000000 --- a/image/dot_red.png +++ /dev/null Binary files differ diff --git a/image/dot_gray.png b/image/dot_gray.png deleted file mode 100644 index 57254db..0000000 --- a/image/dot_gray.png +++ /dev/null Binary files differ diff --git a/image/dot_green.png b/image/dot_green.png deleted file mode 100644 index 05f439d..0000000 --- a/image/dot_green.png +++ /dev/null Binary files differ diff --git a/image/dot_red.png b/image/dot_red.png deleted file mode 100644 index bc64f7c..0000000 --- a/image/dot_red.png +++ /dev/null Binary files differ diff --git a/src/com/casic/swing/ui/TimeGuardNtp.form b/src/com/casic/swing/ui/TimeGuardNtp.form index 31efb19..c3a0702 100644 --- a/src/com/casic/swing/ui/TimeGuardNtp.form +++ b/src/com/casic/swing/ui/TimeGuardNtp.form @@ -2,7 +2,7 @@
- + @@ -24,12 +24,12 @@ - + - + @@ -49,7 +49,7 @@ - + @@ -58,7 +58,7 @@ - + @@ -66,16 +66,6 @@ - - - - - - - - - - @@ -88,7 +78,7 @@ - + @@ -99,7 +89,7 @@ - + @@ -122,7 +112,7 @@ - + @@ -131,6 +121,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/image/dot_gray.png b/image/dot_gray.png deleted file mode 100644 index 57254db..0000000 --- a/image/dot_gray.png +++ /dev/null Binary files differ diff --git a/image/dot_green.png b/image/dot_green.png deleted file mode 100644 index 05f439d..0000000 --- a/image/dot_green.png +++ /dev/null Binary files differ diff --git a/image/dot_red.png b/image/dot_red.png deleted file mode 100644 index bc64f7c..0000000 --- a/image/dot_red.png +++ /dev/null Binary files differ diff --git a/src/com/casic/swing/ui/TimeGuardNtp.form b/src/com/casic/swing/ui/TimeGuardNtp.form index 31efb19..c3a0702 100644 --- a/src/com/casic/swing/ui/TimeGuardNtp.form +++ b/src/com/casic/swing/ui/TimeGuardNtp.form @@ -2,7 +2,7 @@
- + @@ -24,12 +24,12 @@ - + - + @@ -49,7 +49,7 @@ - + @@ -58,7 +58,7 @@ - + @@ -66,16 +66,6 @@ - - - - - - - - - - @@ -88,7 +78,7 @@ - + @@ -99,7 +89,7 @@ - + @@ -122,7 +112,7 @@ - + @@ -131,6 +121,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/casic/swing/ui/TimeGuardNtp.java b/src/com/casic/swing/ui/TimeGuardNtp.java index 7140412..285b6f1 100644 --- a/src/com/casic/swing/ui/TimeGuardNtp.java +++ b/src/com/casic/swing/ui/TimeGuardNtp.java @@ -30,17 +30,18 @@ private JLabel currentTimeLabel; private JCheckBox autoCheckBox; private JComboBox periodComboBox; + private JPanel stateView; private static final String[] HOST_NAME = {"NTP服务器(上海)", "中国国家授时中心", "清华大学", "北京大学", "自定义"}; - private static final String[] HOST_IP = {"ntp.api.bz", "210.72.145.44", "s1b.time.edu.cn", "s1c.time.edu.cn"}; + private static 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 boolean hasNtp = false; - private String host = HOST_IP[0]; private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor( 1, new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build()); + private boolean hasNtp = false; + private String host = HOST_IP[0]; public static void main(String[] args) { TimeGuardNtp timeGuard = new TimeGuardNtp(); @@ -56,7 +57,7 @@ //内边距 timeGuard.ntpPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); timeGuard.dotView.setText("未同步"); - timeGuard.dotView.setIcon(new ImageIcon("image/dot_gray.png")); + setStateView(timeGuard, Color.GRAY); /** * 时间间隔,单位为毫秒 @@ -97,7 +98,6 @@ timeGuard.host = HOST_IP[i]; } } - } } } @@ -170,13 +170,13 @@ boolean isSuccess = result.contains("step time server") || result.contains("adjust time server"); if (isSuccess) { String systemTime = TimeOrDateUtil.timestampToTime(System.currentTimeMillis()); - timeGuard.dotView.setIcon(new ImageIcon("image/dot_green.png")); + setStateView(timeGuard, Color.GREEN); timeGuard.dotView.setText("同步成功"); timeGuard.timeValueLabel.setText(systemTime); //同步成功之后将时间存入本地 StringHelper.saveAssertsData(systemTime); } else { - timeGuard.dotView.setIcon(new ImageIcon("image/dot_red.png")); + setStateView(timeGuard, Color.RED); timeGuard.dotView.setText("同步失败"); timeGuard.timeValueLabel.setText(""); } @@ -190,4 +190,9 @@ timeGuard.periodComboBox.addItem(integer); } } + + private static void setStateView(TimeGuardNtp timeGuard, Color color) { + timeGuard.stateView.setPreferredSize(new Dimension(15, 15)); + timeGuard.stateView.setBackground(color); + } } diff --git a/image/dot_gray.png b/image/dot_gray.png deleted file mode 100644 index 57254db..0000000 --- a/image/dot_gray.png +++ /dev/null Binary files differ diff --git a/image/dot_green.png b/image/dot_green.png deleted file mode 100644 index 05f439d..0000000 --- a/image/dot_green.png +++ /dev/null Binary files differ diff --git a/image/dot_red.png b/image/dot_red.png deleted file mode 100644 index bc64f7c..0000000 --- a/image/dot_red.png +++ /dev/null Binary files differ diff --git a/src/com/casic/swing/ui/TimeGuardNtp.form b/src/com/casic/swing/ui/TimeGuardNtp.form index 31efb19..c3a0702 100644 --- a/src/com/casic/swing/ui/TimeGuardNtp.form +++ b/src/com/casic/swing/ui/TimeGuardNtp.form @@ -2,7 +2,7 @@
- + @@ -24,12 +24,12 @@ - + - + @@ -49,7 +49,7 @@ - + @@ -58,7 +58,7 @@ - + @@ -66,16 +66,6 @@ - - - - - - - - - - @@ -88,7 +78,7 @@ - + @@ -99,7 +89,7 @@ - + @@ -122,7 +112,7 @@ - + @@ -131,6 +121,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/casic/swing/ui/TimeGuardNtp.java b/src/com/casic/swing/ui/TimeGuardNtp.java index 7140412..285b6f1 100644 --- a/src/com/casic/swing/ui/TimeGuardNtp.java +++ b/src/com/casic/swing/ui/TimeGuardNtp.java @@ -30,17 +30,18 @@ private JLabel currentTimeLabel; private JCheckBox autoCheckBox; private JComboBox periodComboBox; + private JPanel stateView; private static final String[] HOST_NAME = {"NTP服务器(上海)", "中国国家授时中心", "清华大学", "北京大学", "自定义"}; - private static final String[] HOST_IP = {"ntp.api.bz", "210.72.145.44", "s1b.time.edu.cn", "s1c.time.edu.cn"}; + private static 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 boolean hasNtp = false; - private String host = HOST_IP[0]; private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor( 1, new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build()); + private boolean hasNtp = false; + private String host = HOST_IP[0]; public static void main(String[] args) { TimeGuardNtp timeGuard = new TimeGuardNtp(); @@ -56,7 +57,7 @@ //内边距 timeGuard.ntpPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); timeGuard.dotView.setText("未同步"); - timeGuard.dotView.setIcon(new ImageIcon("image/dot_gray.png")); + setStateView(timeGuard, Color.GRAY); /** * 时间间隔,单位为毫秒 @@ -97,7 +98,6 @@ timeGuard.host = HOST_IP[i]; } } - } } } @@ -170,13 +170,13 @@ boolean isSuccess = result.contains("step time server") || result.contains("adjust time server"); if (isSuccess) { String systemTime = TimeOrDateUtil.timestampToTime(System.currentTimeMillis()); - timeGuard.dotView.setIcon(new ImageIcon("image/dot_green.png")); + setStateView(timeGuard, Color.GREEN); timeGuard.dotView.setText("同步成功"); timeGuard.timeValueLabel.setText(systemTime); //同步成功之后将时间存入本地 StringHelper.saveAssertsData(systemTime); } else { - timeGuard.dotView.setIcon(new ImageIcon("image/dot_red.png")); + setStateView(timeGuard, Color.RED); timeGuard.dotView.setText("同步失败"); timeGuard.timeValueLabel.setText(""); } @@ -190,4 +190,9 @@ timeGuard.periodComboBox.addItem(integer); } } + + private static void setStateView(TimeGuardNtp timeGuard, Color color) { + timeGuard.stateView.setPreferredSize(new Dimension(15, 15)); + timeGuard.stateView.setBackground(color); + } } diff --git a/src/com/casic/swing/utils/CommandUtil.java b/src/com/casic/swing/utils/CommandUtil.java index c23cf2b..c487e7d 100644 --- a/src/com/casic/swing/utils/CommandUtil.java +++ b/src/com/casic/swing/utils/CommandUtil.java @@ -15,6 +15,7 @@ * 检查当前系统是否已安装NTP */ public static boolean checkEnv() { + System.out.println("检查当前系统是否支持NTP"); try { Process exec = Runtime.getRuntime().exec("rpm -q ntp"); try { @@ -24,7 +25,6 @@ e.printStackTrace(); } String result = parseResult(exec.getInputStream()); - System.out.println("命令执行结果 ===> " + result); return !result.isEmpty(); } catch (IOException e) { e.printStackTrace(); @@ -43,7 +43,9 @@ */ public static String ntpDate(String host) { try { - Process exec = Runtime.getRuntime().exec("ntpdate -u " + host); + String command = "ntpdate -u " + host; + System.out.println("授时同步 ===> " + command); + Process exec = Runtime.getRuntime().exec(command); try { //等待命令执行完成 exec.waitFor(5, TimeUnit.SECONDS);