項目結構:
運行效果:
========================================================
代碼部分:
========================================================
/shutdownApp/src/com/b510/examples/Shutdown.java
1 package com.b510.examples; 2 3 import com.b510.examples.window.dao.MyWindowDao; 4 import com.b510.examples.window.impl.MyWindowImpl; 5 6 /** 7 * @author Hongten 8 * 9 * @time 2011-9-3 2011 10 */ 11 public class Shutdown { 12 public static void main(String[] args) { 13 @SuppressWarnings("unused") 14 MyWindowDao dao=new MyWindowImpl("Hongten-v1.0.定時關機"); 15 } 16 }
/shutdownApp/src/com/b510/examples/window/component/MyBox.java
1 package com.b510.examples.window.component; 2 3 import javax.swing.Box; 4 5 /** 6 * @author Hongten 7 * 8 * @time 2011-9-5 2011 9 */ 10 public class MyBox { 11 Box left=Box.createVerticalBox(); 12 Box center=Box.createVerticalBox(); 13 Box right=Box.createVerticalBox(); 14 Box middle_part=Box.createHorizontalBox(); 15 /** 16 * @return the left 17 */ 18 public Box getLeft() { 19 return left; 20 } 21 /** 22 * @param left the left to set 23 */ 24 public void setLeft(Box left) { 25 this.left = left; 26 } 27 /** 28 * @return the center 29 */ 30 public Box getCenter() { 31 return center; 32 } 33 /** 34 * @param center the center to set 35 */ 36 public void setCenter(Box center) { 37 this.center = center; 38 } 39 /** 40 * @return the right 41 */ 42 public Box getRight() { 43 return right; 44 } 45 /** 46 * @param right the right to set 47 */ 48 public void setRight(Box right) { 49 this.right = right; 50 } 51 /** 52 * @return the middle_part 53 */ 54 public Box getMiddle_part() { 55 return middle_part; 56 } 57 /** 58 * @param middlePart the middle_part to set 59 */ 60 public void setMiddle_part(Box middlePart) { 61 middle_part = middlePart; 62 } 63 64 65 }
/shutdownApp/src/com/b510/examples/window/component/MyButton.java
1 package com.b510.examples.window.component; 2 3 import javax.swing.JButton; 4 5 /** 6 * @author Hongten 7 * 8 * @time 2011-9-5 2011 9 */ 10 public class MyButton { 11 JButton shutdown = new JButton("關機"); 12 JButton restart = new JButton("重啟"); 13 JButton logout = new JButton("注銷"); 14 JButton clear = new JButton("清零"); 15 JButton cancel = new JButton("取消"); 16 JButton immshutdown = new JButton("立刻關機"); 17 18 /** 19 * @return the shutdown 20 */ 21 public JButton getShutdown() { 22 return shutdown; 23 } 24 25 /** 26 * @param shutdown 27 * the shutdown to set 28 */ 29 public void setShutdown(JButton shutdown) { 30 this.shutdown = shutdown; 31 } 32 33 /** 34 * @return the restart 35 */ 36 public JButton getRestart() { 37 return restart; 38 } 39 40 /** 41 * @param restart 42 * the restart to set 43 */ 44 public void setRestart(JButton restart) { 45 this.restart = restart; 46 } 47 48 /** 49 * @return the logout 50 */ 51 public JButton getLogout() { 52 return logout; 53 } 54 55 /** 56 * @param logout 57 * the logout to set 58 */ 59 public void setLogout(JButton logout) { 60 this.logout = logout; 61 } 62 63 /** 64 * @return the clear 65 */ 66 public JButton getClear() { 67 return clear; 68 } 69 70 /** 71 * @param clear 72 * the clear to set 73 */ 74 public void setClear(JButton clear) { 75 this.clear = clear; 76 } 77 78 /** 79 * @return the cancel 80 */ 81 public JButton getCancel() { 82 return cancel; 83 } 84 85 /** 86 * @param cancel 87 * the cancel to set 88 */ 89 public void setCancel(JButton cancel) { 90 this.cancel = cancel; 91 } 92 93 /** 94 * @return the immshutdown 95 */ 96 public JButton getImmshutdown() { 97 return immshutdown; 98 } 99 100 /** 101 * @param immshutdown 102 * the immshutdown to set 103 */ 104 public void setImmshutdown(JButton immshutdown) { 105 this.immshutdown = immshutdown; 106 } 107 108 }
/shutdownApp/src/com/b510/examples/window/component/MyLabel.java
1 package com.b510.examples.window.component; 2 3 import javax.swing.JLabel; 4 5 /** 6 * @author Hongten 7 * 8 * @time 2011-9-5 2011 9 */ 10 public class MyLabel { 11 // 標簽 現在時間 ,設置小時,設置分鍾,設置秒鍾,概念,工作室,剩余 12 JLabel time_now = new JLabel(); 13 JLabel set_hour = new JLabel("設置小時"); 14 JLabel set_min = new JLabel("設置分鍾"); 15 JLabel set_sec = new JLabel("設置秒鍾"); 16 JLabel concept = new JLabel("Hongten-B510"); 17 JLabel workspace = new JLabel("Hongtenzone@foxmain.com"); 18 JLabel surplus = new JLabel(); 19 20 /** 21 * @return the time_now 22 */ 23 public JLabel getTime_now() { 24 return time_now; 25 } 26 27 /** 28 * @param timeNow 29 * the time_now to set 30 */ 31 public void setTime_now(JLabel timeNow) { 32 time_now = timeNow; 33 } 34 35 /** 36 * @return the set_hour 37 */ 38 public JLabel getSet_hour() { 39 return set_hour; 40 } 41 42 /** 43 * @param setHour 44 * the set_hour to set 45 */ 46 public void setSet_hour(JLabel setHour) { 47 set_hour = setHour; 48 } 49 50 /** 51 * @return the set_min 52 */ 53 public JLabel getSet_min() { 54 return set_min; 55 } 56 57 /** 58 * @param setMin 59 * the set_min to set 60 */ 61 public void setSet_min(JLabel setMin) { 62 set_min = setMin; 63 } 64 65 /** 66 * @return the set_sec 67 */ 68 public JLabel getSet_sec() { 69 return set_sec; 70 } 71 72 /** 73 * @param setSec 74 * the set_sec to set 75 */ 76 public void setSet_sec(JLabel setSec) { 77 set_sec = setSec; 78 } 79 80 /** 81 * @return the concept 82 */ 83 public JLabel getConcept() { 84 return concept; 85 } 86 87 /** 88 * @param concept 89 * the concept to set 90 */ 91 public void setConcept(JLabel concept) { 92 this.concept = concept; 93 } 94 95 /** 96 * @return the workspace 97 */ 98 public JLabel getWorkspace() { 99 return workspace; 100 } 101 102 /** 103 * @param workspace 104 * the workspace to set 105 */ 106 public void setWorkspace(JLabel workspace) { 107 this.workspace = workspace; 108 } 109 110 /** 111 * @return the surplus 112 */ 113 public JLabel getSurplus() { 114 return surplus; 115 } 116 117 /** 118 * @param surplus 119 * the surplus to set 120 */ 121 public void setSurplus(JLabel surplus) { 122 this.surplus = surplus; 123 } 124 125 }
/shutdownApp/src/com/b510/examples/window/component/MyPanel.java
1 package com.b510.examples.window.component; 2 3 import javax.swing.JPanel; 4 5 /** 6 * @author Hongten 7 * 8 * @time 2011-9-5 2011 9 */ 10 public class MyPanel { 11 JPanel buttom=new JPanel(); 12 13 /** 14 * @return the buttom 15 */ 16 public JPanel getButtom() { 17 return buttom; 18 } 19 20 /** 21 * @param buttom the buttom to set 22 */ 23 public void setButtom(JPanel buttom) { 24 this.buttom = buttom; 25 } 26 27 }
/shutdownApp/src/com/b510/examples/window/component/MyTextField.java
1 package com.b510.examples.window.component; 2 3 import javax.swing.JTextField; 4 5 /** 6 * @author Hongten 7 * 8 * @time 2011-9-5 2011 9 */ 10 public class MyTextField { 11 // 文本框 時,分,秒 12 static JTextField hour = new JTextField(10); 13 static JTextField min = new JTextField(10); 14 static JTextField sec = new JTextField(10); 15 16 /** 17 * @return the hour 18 */ 19 public JTextField getHour() { 20 return hour; 21 } 22 23 /** 24 * @param hour 25 * the hour to set 26 */ 27 public void setHour(JTextField hour) { 28 MyTextField.hour = hour; 29 } 30 31 /** 32 * @return the min 33 */ 34 public JTextField getMin() { 35 return min; 36 } 37 38 /** 39 * @param min 40 * the min to set 41 */ 42 public void setMin(JTextField min) { 43 MyTextField.min = min; 44 } 45 46 /** 47 * @return the sec 48 */ 49 public JTextField getSec() { 50 return sec; 51 } 52 53 /** 54 * @param sec 55 * the sec to set 56 */ 57 public void setSec(JTextField sec) { 58 MyTextField.sec = sec; 59 } 60 61 }
/shutdownApp/src/com/b510/examples/window/dao/MyWindowDao.java
1 package com.b510.examples.window.dao; 2 3 /** 4 * @author Hongten 5 * 6 * @time 2011-9-5 2011 7 */ 8 public interface MyWindowDao { 9 10 /** 11 * 得到系統時間並顯示 設置在窗體頭部的信息 12 * 13 * @return 14 */ 15 public String now(); 16 17 /** 18 * 得到剩余時間並顯示 19 * 20 * @return 21 */ 22 public String surplus_time(); 23 24 /** 25 * 關機操作 26 */ 27 public void shutDown(); 28 29 /** 30 * 重啟操作 31 */ 32 public void restart(); 33 34 /** 35 * 注銷操作 36 */ 37 public void logOut(); 38 39 /** 40 * 取消操作 41 */ 42 public void cancel(); 43 44 /** 45 * 我們先設置好時間,一旦時間一到, 就調用此方法 該方法中可以實現關機,重啟,注銷等操作 46 */ 47 public void thenExecutive(); 48 49 /** 50 * 自己定義的監聽器類, 監聽我們設置的關機或重啟或注銷時間是否到時了 如果到時,就執行相應的動作(關機或重啟或注銷) 51 */ 52 public void myListener(); 53 54 /** 55 * 時間監聽器,得到系統時間和設置好時間后,得到剩余時間 56 */ 57 public void timeListener(); 58 59 /** 60 * 要執行的動作 61 */ 62 public void myAction(); 63 64 /** 65 * 判斷三個文本域(hour,min,sec)中的時間是否合法 66 * 67 * @return 68 */ 69 70 public boolean judgeTime(); 71 72 /** 73 * 判斷是否為純數字,如果是返回true,否則返回false 74 * 75 * @param str 76 * 為一個String類型 77 * @return 78 */ 79 public boolean isAllNumber(String str); 80 }
/shutdownApp/src/com/b510/examples/window/dao/MyWindowLayoutDao.java
1 package com.b510.examples.window.dao; 2 3 /** 4 * @author Hongten 5 * 6 * @time 2011-9-5 2011 7 */ 8 public interface MyWindowLayoutDao { 9 /** 10 * 所有文本域屬性設置 11 */ 12 public void setTextFieldAddListener(); 13 14 /** 15 * 所有的JLabel屬性設置 16 */ 17 public void setLabel(); 18 19 /** 20 * 所有的JButton屬性設置 21 */ 22 public void setButtonAddListener(); 23 24 /** 25 * 窗體布局設置 26 */ 27 public void setLayout(); 28 }
/shutdownApp/src/com/b510/examples/window/dao/SetMyLayoutDao.java
1 package com.b510.examples.window.dao; 2 3 import com.b510.examples.window.component.MyLabel; 4 import com.b510.examples.window.component.MyTextField; 5 6 /** 7 * @author Hongten 8 * 9 * @time 2011-9-5 2011 10 */ 11 public interface SetMyLayoutDao { 12 /** 13 * 三個文本域(hour,min,sec)屬性的相關設置 14 */ 15 public void setTextField(MyTextField myTextField); 16 /** 17 * 所有的JLabel屬性設置 18 */ 19 public void setLabel(MyLabel myLabel); 20 }
/shutdownApp/src/com/b510/examples/window/impl/MyWindowImpl.java
這個是小程序的主要功能類,不過在時間處理方面不是很完善
不過在之后的小鬧鍾程序中就得以完善了,大家可以看看小鬧鍾程序的時間處理:
http://www.cnblogs.com/hongten/archive/2012/06/02/java_alarm.html
1 package com.b510.examples.window.impl; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.Container; 6 import java.awt.Dimension; 7 import java.awt.GridLayout; 8 import java.awt.Toolkit; 9 import java.awt.event.ActionEvent; 10 import java.awt.event.ActionListener; 11 import java.io.IOException; 12 import java.util.Calendar; 13 import java.util.GregorianCalendar; 14 15 import javax.swing.Box; 16 import javax.swing.JFrame; 17 import javax.swing.JOptionPane; 18 import javax.swing.event.DocumentEvent; 19 import javax.swing.event.DocumentListener; 20 21 import com.b510.examples.window.component.MyBox; 22 import com.b510.examples.window.component.MyButton; 23 import com.b510.examples.window.component.MyLabel; 24 import com.b510.examples.window.component.MyPanel; 25 import com.b510.examples.window.component.MyTextField; 26 import com.b510.examples.window.dao.MyWindowDao; 27 import com.b510.examples.window.dao.MyWindowLayoutDao; 28 import com.b510.examples.window.dao.SetMyLayoutDao; 29 30 /** 31 * @author Hongten 32 * 33 * @time 2011-9-3 2011 34 */ 35 public class MyWindowImpl extends JFrame implements ActionListener, 36 DocumentListener, MyWindowDao, MyWindowLayoutDao { 37 private static final long serialVersionUID = -7018374117422042620L; 38 39 MyTextField myTextField = new MyTextField(); 40 MyLabel myLabel = new MyLabel(); 41 MyButton myButton = new MyButton(); 42 MyBox myBox = new MyBox(); 43 MyPanel buttom = new MyPanel(); 44 Container container; 45 int flag; 46 47 public MyWindowImpl(String title) { 48 super(title);// 設置標題 49 init(); 50 } 51 52 /** 53 * 初始化窗體 54 */ 55 public void init() { 56 this.setVisible(true); 57 this.setResizable(false);// 設置大小不可變 58 this.setSize(525, 215); 59 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 60 this.setLocation(screen.width - 525, 0);// 設置窗體的初始位置 61 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設置窗體可以關閉 62 SetMyLayoutDao setMyLayout = new SetMyLayoutImpl(); 63 setLabel();// JLabel標簽屬性設置 64 timeListener();// 系統時間監聽器 65 setMyLayout.setTextField(myTextField); 66 setMyLayout.setLabel(myLabel); 67 setTextFieldAddListener();// JTextField文本域屬性設置 68 setButtonAddListener();// JButton按鈕屬性設置 69 setLayout();// 窗體布局設置 70 } 71 72 /** 73 * 設置文本域的監聽器 74 */ 75 public void setTextFieldAddListener() { 76 // 注入監聽器 77 myTextField.getHour().addActionListener(this); 78 (myTextField.getHour().getDocument()).addDocumentListener(this); 79 80 // 注入監聽器 81 myTextField.getMin().addActionListener(this); 82 (myTextField.getMin().getDocument()).addDocumentListener(this); 83 84 // 注入監聽器 85 myTextField.getSec().addActionListener(this); 86 (myTextField.getSec().getDocument()).addDocumentListener(this); 87 88 } 89 90 /** 91 * 所有的JLabel屬性設置 92 */ 93 public void setLabel() { 94 // 現在時間 頭部顯示 95 myLabel.getTime_now().setText("SystemTime:" + now()); 96 } 97 98 /** 99 * 所有的JButton屬性設置 100 */ 101 public void setButtonAddListener() { 102 // 設置按鈕的一些屬性,可用,添加監聽器 103 myButton.getShutdown().setEnabled(false);// 關機 104 myButton.getShutdown().addActionListener(this); 105 myButton.getRestart().setEnabled(false);// 重啟 106 myButton.getRestart().addActionListener(this); 107 myButton.getLogout().setEnabled(false);// 注銷 108 myButton.getLogout().addActionListener(this); 109 myButton.getClear().setEnabled(false);// 清零 110 myButton.getClear().addActionListener(this); 111 myButton.getCancel().setEnabled(false);// 取消 112 myButton.getCancel().addActionListener(this); 113 myButton.getImmshutdown().addActionListener(this);// 立刻關機 114 } 115 116 /** 117 * 窗體布局設置 118 */ 119 public void setLayout() { 120 // 左邊布局 121 myBox.getLeft().add(myLabel.getSet_hour()); 122 myBox.getLeft().add(Box.createVerticalStrut(20)); 123 myBox.getLeft().add(myLabel.getSet_min()); 124 myBox.getLeft().add(Box.createVerticalStrut(20)); 125 myBox.getLeft().add(myLabel.getSet_sec()); 126 // 中間布局 127 128 myBox.getCenter().add(myTextField.getHour()); 129 myBox.getCenter().add(Box.createVerticalStrut(9)); 130 myBox.getCenter().add(myTextField.getMin()); 131 myBox.getCenter().add(Box.createVerticalStrut(9)); 132 myBox.getCenter().add(myTextField.getSec()); 133 134 // 右邊布局 135 myBox.getRight().add(myButton.getShutdown()); 136 myBox.getRight().add(Box.createVerticalStrut(10)); 137 myBox.getRight().add(myButton.getRestart()); 138 myBox.getRight().add(Box.createVerticalStrut(10)); 139 myBox.getRight().add(myButton.getLogout()); 140 validate(); 141 // 中部 142 myBox.getMiddle_part().add(myBox.getLeft()); 143 myBox.getMiddle_part().add(Box.createHorizontalStrut(5)); 144 myBox.getMiddle_part().add(myBox.getCenter()); 145 myBox.getMiddle_part().add(Box.createHorizontalStrut(5)); 146 myBox.getMiddle_part().add(myBox.getRight()); 147 myBox.getMiddle_part().validate(); 148 // 底部 149 buttom.getButtom().setLayout(new GridLayout(2, 3));// 采用網格布局 2行 ,3列 150 buttom.getButtom().setBackground(Color.white); 151 buttom.getButtom().add(myButton.getClear()); 152 buttom.getButtom().add(myButton.getCancel()); 153 buttom.getButtom().add(myButton.getImmshutdown()); 154 buttom.getButtom().add(myLabel.getConcept()); 155 buttom.getButtom().add(myLabel.getSurplus()); 156 buttom.getButtom().add(myLabel.getWorkspace()); 157 buttom.getButtom().validate(); 158 // 容器 159 container = getContentPane(); 160 container.setLayout(new BorderLayout()); 161 container.add(myLabel.getTime_now(), BorderLayout.NORTH); 162 container.add(myBox.getMiddle_part(), BorderLayout.CENTER); 163 container.add(buttom.getButtom(), BorderLayout.SOUTH); 164 container.setBackground(Color.white); 165 container.validate(); 166 } 167 168 /** 169 * 得到系統時間並顯示 設置在窗體頭部的信息 170 * 171 * @return 172 */ 173 public String now() { 174 Calendar cal = Calendar.getInstance(); 175 int year = cal.get(Calendar.YEAR); 176 int month = cal.get(Calendar.MONTH) + 1; 177 int day = cal.get(Calendar.DAY_OF_MONTH); 178 int hour = cal.get(Calendar.HOUR_OF_DAY); 179 int min = cal.get(Calendar.MINUTE); 180 int sec = cal.get(Calendar.SECOND); 181 String current = new String(year + "-" + month + "-" + day + " " + hour 182 + ":" + min + ":" + sec); 183 myLabel.getTime_now().setText("SystemTime:" + current); 184 return current; 185 } 186 187 /** 188 * 得到剩余時間並顯示 189 * 190 * 由於我們的文本域(hour,min,sec)都有客戶端直接輸入,可能輸入的不是純數字 這樣我們在監聽的時候, 比如:Integer 191 * int_hour = Integer.parseInt(hour.getText()); 就會報錯,為了解決這個錯誤,我們制定了以下方法, 192 * 判斷文本域中的內容是否為純數字,如果不是,則出現提示信息, 並且將文本內容設置為當前相應的時間 193 * 194 * @return 195 */ 196 public String surplus_time() { 197 Calendar cal = Calendar.getInstance();// new GregorianCalendar(); 198 int sur_hour = cal.get(Calendar.HOUR_OF_DAY); 199 int sur_min = cal.get(Calendar.MINUTE); 200 int sur_sec = cal.get(Calendar.SECOND); 201 if (!isAllNumber(myTextField.getHour().getText())) { 202 JOptionPane.showMessageDialog(this, 203 "時間設置滿足:0=<小時<24,0=<分鍾<60,0=<秒<60!", "溫馨提示", 204 JOptionPane.WARNING_MESSAGE); 205 myTextField.getHour().setText(String.valueOf(sur_hour));// 設置當前小時 206 myTextField.getHour().requestFocus(); // 文本框 時 獲得焦點 207 return null; 208 } else if (!isAllNumber(myTextField.getMin().getText())) { 209 JOptionPane.showMessageDialog(this, 210 "時間設置滿足:0=<小時<24,0=<分鍾<60,0=<秒<60!", "溫馨提示", 211 JOptionPane.WARNING_MESSAGE); 212 myTextField.getMin().setText(String.valueOf(sur_min));// 設置當前分鍾 213 myTextField.getMin().requestFocus(); // 文本框 時 獲得焦點 214 return null; 215 } else if (!isAllNumber(myTextField.getSec().getText())) { 216 JOptionPane.showMessageDialog(this, 217 "時間設置滿足:0=<小時<24,0=<分鍾<60,0=<秒<60!", "溫馨提示", 218 JOptionPane.WARNING_MESSAGE); 219 myTextField.getSec().setText(String.valueOf(sur_sec));// 設置當前秒鍾 220 myTextField.getSec().requestFocus(); // 文本框 時 獲得焦點 221 return null; 222 } else { 223 Integer int_hour = Integer 224 .parseInt(myTextField.getHour().getText()); 225 Integer int_min = Integer.parseInt(myTextField.getMin().getText()); 226 Integer int_sec = Integer.parseInt(myTextField.getSec().getText()); 227 int hour = int_hour.intValue() - sur_hour; 228 int min = int_min.intValue() - sur_min; 229 int sec = int_sec.intValue() - sur_sec; 230 if (sec < 0) { 231 if (min > 0 && hour >= 0) { 232 sec += 60; 233 min = min - 1; 234 } 235 if (min == 0 && hour > 0) { 236 min += 60; 237 hour -= 1; 238 } 239 } else if (min < 0) { 240 if (hour > 0) { 241 min += 60; 242 hour = hour - 1; 243 } 244 } else if (hour == 0 && min == 0 && sec < 0) { 245 myLabel.getSurplus().setVisible(false); 246 } 247 String sur = new String("剩余:" + hour + "時" + min + "分" + sec + "秒"); 248 myLabel.getSurplus().setText(sur); 249 return sur; 250 } 251 252 } 253 254 /** 255 * 重寫DocumentListener的方法 該方法監聽三個文本域(hour,min,sec)是否已經填寫好 256 * 如果填寫好,一些按鈕就會被觸發可用,否則不可用 257 */ 258 @Override 259 public void changedUpdate(DocumentEvent e) {// 260 if (myTextField.getHour().getText().equals("") 261 || myTextField.getMin().getText().equals("") 262 || myTextField.getSec().getText().equals("")) { 263 myButton.getShutdown().setEnabled(false); 264 myButton.getRestart().setEnabled(false); 265 myButton.getLogout().setEnabled(false); 266 myButton.getClear().setEnabled(false); 267 } else { 268 myButton.getShutdown().setEnabled(true); 269 myButton.getRestart().setEnabled(true); 270 myButton.getLogout().setEnabled(true); 271 myButton.getClear().setEnabled(true); 272 } 273 } 274 275 /** 276 * 重寫DocumentListener的方法 277 */ 278 @Override 279 public void insertUpdate(DocumentEvent e) { 280 changedUpdate(e); 281 282 } 283 284 /** 285 * 重寫DocumentListener的方法 286 */ 287 @Override 288 public void removeUpdate(DocumentEvent e) { 289 changedUpdate(e); 290 } 291 292 /** 293 * 關機操作 294 */ 295 public void shutDown() { 296 try { 297 Runtime.getRuntime().exec("shutdown.exe -s -t 1"); 298 } catch (IOException e) { 299 JOptionPane.showMessageDialog(this, "關機執行失敗!"); 300 } 301 } 302 303 /** 304 * 重啟操作 305 */ 306 public void restart() { 307 try { 308 Runtime.getRuntime().exec("shutdown.exe -r -t 1"); 309 } catch (IOException e) { 310 JOptionPane.showMessageDialog(this, "重啟執行失敗!"); 311 } 312 } 313 314 /** 315 * 注銷操作 316 */ 317 public void logOut() { 318 try { 319 Runtime.getRuntime().exec("shutdown.exe -l"); 320 } catch (IOException e) { 321 JOptionPane.showMessageDialog(this, "注銷執行失敗!"); 322 } 323 } 324 325 /** 326 * 取消操作 327 */ 328 public void cancel() { 329 try { 330 Runtime.getRuntime().exec("shutdown.exe -a"); 331 } catch (IOException e) { 332 JOptionPane.showMessageDialog(this, "取消執行失敗!"); 333 } 334 } 335 336 /** 337 * 我們先設置好時間,一旦時間一到, 就調用此方法 該方法中可以實現關機,重啟,注銷等操作 338 */ 339 public void thenExecutive() { 340 Calendar cal = new GregorianCalendar(); 341 int then_hour = cal.get(Calendar.HOUR_OF_DAY); 342 int then_min = cal.get(Calendar.MINUTE); 343 int then_sec = cal.get(Calendar.SECOND); 344 // 這里要說明的是: 345 // 由於我們的文本域(hour,min,sec)都有客戶端直接輸入,可能輸入的不是純數字 346 // 這樣我們在監聽的時候, 347 // 比如:Integer int_hour = Integer.parseInt(hour.getText()); 348 // 就會報錯,為了解決這個錯誤,我們制定了以下方法, 349 // 判斷文本域中的內容是否為純數字,如果不是,則出現提示信息, 350 // 並且將文本內容設置為當前相應的時間 351 if (!isAllNumber(myTextField.getHour().getText())) { 352 JOptionPane.showMessageDialog(this, 353 "時間設置滿足:0=<小時<24,0=<分鍾<60,0=<秒<60!", "溫馨提示", 354 JOptionPane.WARNING_MESSAGE); 355 myTextField.getHour().setText(String.valueOf(then_hour));// 設置當前小時 356 myTextField.getHour().requestFocus(); // 文本框 時 獲得焦點 357 } else if (!isAllNumber(myTextField.getMin().getText())) { 358 JOptionPane.showMessageDialog(this, 359 "時間設置滿足:0=<小時<24,0=<分鍾<60,0=<秒<60!", "溫馨提示", 360 JOptionPane.WARNING_MESSAGE); 361 myTextField.getMin().setText(String.valueOf(then_min));// 設置當前分鍾 362 myTextField.getMin().requestFocus(); // 文本框 時 獲得焦點 363 } else if (!isAllNumber(myTextField.getSec().getText())) { 364 JOptionPane.showMessageDialog(this, 365 "時間設置滿足:0=<小時<24,0=<分鍾<60,0=<秒<60!", "溫馨提示", 366 JOptionPane.WARNING_MESSAGE); 367 myTextField.getSec().setText(String.valueOf(then_sec));// 設置當前秒鍾 368 myTextField.getSec().requestFocus(); // 文本框 時 獲得焦點 369 } else { 370 Integer int_hour = Integer 371 .parseInt(myTextField.getHour().getText()); 372 Integer int_min = Integer.parseInt(myTextField.getMin().getText()); 373 Integer int_sec = Integer.parseInt(myTextField.getSec().getText()); 374 375 if (then_hour == int_hour.intValue() 376 && then_min == int_min.intValue() 377 && then_sec == int_sec.intValue()) { 378 if (flag == 1) { 379 // 關機 380 shutDown(); 381 myButton.getCancel().setEnabled(true); 382 myButton.getClear().setEnabled(false); 383 } 384 if (flag == 2) { 385 // 重啟 386 restart(); 387 myButton.getCancel().setEnabled(true); 388 myButton.getClear().setEnabled(false); 389 } 390 if (flag == 3) { 391 // 注銷 392 logOut(); 393 myButton.getCancel().setEnabled(true); 394 myButton.getClear().setEnabled(false); 395 } 396 } 397 } 398 } 399 400 /** 401 * 重寫ActionListener的方法 該方法中可以監聽: 立刻關機,到時關機,重啟,注銷,取消,清零等動作 402 */ 403 @Override 404 public void actionPerformed(ActionEvent e) { 405 int action_hour = Integer.parseInt(myTextField.getHour().getText()); 406 int action_min = Integer.parseInt(myTextField.getMin().getText()); 407 int action_sec = Integer.parseInt(myTextField.getSec().getText()); 408 // 立刻關機 409 if (e.getSource() == myButton.getImmshutdown()) { 410 shutDown(); 411 } 412 // 到時候才關機 413 if (e.getSource() == myButton.getShutdown()) { 414 myButton.getShutdown().setEnabled(false); 415 myButton.getRestart().setEnabled(false); 416 myButton.getLogout().setEnabled(false); 417 myButton.getClear().setEnabled(false); 418 myButton.getCancel().setEnabled(true); 419 myLabel.getSurplus().setVisible(true); 420 myTextField.getHour().setEnabled(false); 421 myTextField.getMin().setEnabled(false); 422 myTextField.getSec().setEnabled(false); 423 flag = 1; 424 if (action_hour >= 0 && action_hour <= 23 && action_min >= 0 425 && action_min <= 59 && action_sec >= 0 && action_sec <= 59) { 426 myAction(); 427 } else { 428 JOptionPane.showMessageDialog(this, 429 "時間設置滿足:0=<小時<24,0=<分鍾<60,0=<秒<60!", "溫馨提示", 430 JOptionPane.WARNING_MESSAGE); 431 myTextField.getHour().requestFocus(); // 文本框 時 獲得焦點 432 } 433 } 434 // 到時候才重啟 435 if (e.getSource() == myButton.getRestart()) { 436 myButton.getShutdown().setEnabled(false); 437 myButton.getRestart().setEnabled(false); 438 myButton.getLogout().setEnabled(false); 439 myButton.getClear().setEnabled(false); 440 myButton.getCancel().setEnabled(true); 441 myLabel.getSurplus().setVisible(true); 442 myTextField.getHour().setEnabled(false); 443 myTextField.getMin().setEnabled(false); 444 myTextField.getSec().setEnabled(false); 445 flag = 2; 446 if (action_hour >= 0 && action_hour <= 23 && action_min >= 0 447 && action_min <= 59 && action_sec >= 0 && action_sec <= 59) { 448 myAction(); 449 } else { 450 JOptionPane.showMessageDialog(this, 451 "時間設置滿足:0=<小時<24,0=<分鍾<60,0=<秒<60!", "溫馨提示", 452 JOptionPane.WARNING_MESSAGE); 453 myTextField.getHour().requestFocus(); // 文本框 時 獲得焦點 454 } 455 } 456 // 到時候才注銷 457 if (e.getSource() == myButton.getLogout()) { 458 myButton.getShutdown().setEnabled(false); 459 myButton.getRestart().setEnabled(false); 460 myButton.getLogout().setEnabled(false); 461 myButton.getClear().setEnabled(false); 462 myButton.getCancel().setEnabled(true); 463 myLabel.getSurplus().setVisible(true); 464 myTextField.getHour().setEnabled(false); 465 myTextField.getMin().setEnabled(false); 466 myTextField.getSec().setEnabled(false); 467 flag = 3; 468 if (action_hour >= 0 && action_hour <= 23 && action_min >= 0 469 && action_min <= 59 && action_sec >= 0 && action_sec <= 59) { 470 myAction(); 471 } else { 472 JOptionPane.showMessageDialog(this, 473 "時間設置滿足:0=<小時<24,0=<分鍾<60,0=<秒<60!", "溫馨提示", 474 JOptionPane.WARNING_MESSAGE); 475 myButton.getClear().setEnabled(true); 476 myTextField.getHour().requestFocus(); // 文本框 時 獲得焦點 477 } 478 } 479 // 取消 480 if (e.getSource() == myButton.getCancel()) { 481 cancel(); 482 myTextField.getHour().requestFocus(); 483 myButton.getShutdown().setEnabled(true); 484 myButton.getRestart().setEnabled(true); 485 myButton.getLogout().setEnabled(true); 486 myButton.getClear().setEnabled(true); 487 myButton.getCancel().setEnabled(false); 488 myLabel.getSurplus().setVisible(false); 489 myTextField.getHour().setEnabled(true); 490 myTextField.getMin().setEnabled(true); 491 myTextField.getSec().setEnabled(true); 492 } 493 // 清零 494 if (e.getSource() == myButton.getClear()) { 495 myTextField.getHour().setText("0"); 496 myTextField.getMin().setText("0"); 497 myTextField.getSec().setText("0"); 498 myTextField.getHour().requestFocus(); 499 myButton.getClear().setEnabled(false); 500 myLabel.getSurplus().setVisible(false); 501 } 502 } 503 504 /** 505 * 自己定義的監聽器類, 監聽我們設置的關機或重啟或注銷時間是否到時了 如果到時,就執行相應的動作(關機或重啟或注銷) 506 */ 507 public void myListener() { 508 new Thread(new Runnable() { 509 public void run() { 510 while (true) { 511 try { 512 Thread.sleep(1000); 513 } catch (Exception e) { 514 e.printStackTrace(); 515 } 516 thenExecutive();// 關機或重啟或注銷 517 } 518 } 519 }).start(); 520 } 521 522 /** 523 * 時間監聽器,得到系統時間和設置好時間后,得到剩余時間 524 */ 525 public void timeListener() { 526 new Thread(new Runnable() {// 設置一個線程 527 public void run() { 528 while (true) { 529 try { 530 Thread.sleep(1000); 531 } catch (Exception e) { 532 e.printStackTrace(); 533 } 534 now();// 得到系統時間 535 surplus_time();// 得到剩余時間 536 } 537 } 538 }).start(); 539 } 540 541 /** 542 * 要執行的動作 543 */ 544 public void myAction() { 545 if (judgeTime() == false) { 546 JOptionPane.showMessageDialog(this, "時間設定錯誤"); 547 myLabel.getSurplus().setVisible(false); 548 myButton.getShutdown().setEnabled(true); 549 myButton.getRestart().setEnabled(true); 550 myButton.getLogout().setEnabled(true); 551 } else { 552 myListener(); 553 } 554 } 555 556 /** 557 * 判斷三個文本域(hour,min,sec)中的時間是否合法 558 * 559 * @return 560 */ 561 public boolean judgeTime() { 562 boolean boo = true; 563 Calendar cal = new GregorianCalendar(); 564 int judge_hour = cal.get(Calendar.HOUR_OF_DAY); 565 int judge_min = cal.get(Calendar.MINUTE); 566 int judge_sec = cal.get(Calendar.SECOND); 567 int int_hour = Integer.parseInt(myTextField.getHour().getText()); 568 int int_min = Integer.parseInt(myTextField.getMin().getText()); 569 int int_sec = Integer.parseInt(myTextField.getSec().getText()); 570 571 if (int_hour < judge_hour) { 572 boo = false; 573 } else { 574 if (int_hour == judge_hour) { 575 if (int_min < judge_min) { 576 boo = false; 577 } else if (int_min == judge_min) { 578 if (int_sec < judge_sec + 5) { 579 boo = false; 580 } 581 } 582 } 583 } 584 return boo; 585 } 586 587 /** 588 * 判斷是否為純數字,如果是返回true,否則返回false 589 * 590 * @param str 591 * 為一個String類型 592 * @return 593 */ 594 public boolean isAllNumber(String str) { 595 if (str.matches("^[0-9]+$")) { 596 return true; 597 } else { 598 return false; 599 } 600 } 601 }
/shutdownApp/src/com/b510/examples/window/impl/SetMyLayoutImpl.java
package com.b510.examples.window.impl; import java.awt.Color; import java.awt.Font; import java.util.Calendar; import javax.swing.JFrame; import com.b510.examples.window.component.MyLabel; import com.b510.examples.window.component.MyTextField; import com.b510.examples.window.dao.SetMyLayoutDao; /** * @author Hongten * * @time 2011-9-5 2011 */ public class SetMyLayoutImpl extends JFrame implements SetMyLayoutDao { private static final long serialVersionUID = -6414864615563143323L; /** * 三個文本域(hour,min,sec)屬性的相關設置 */ public void setTextField(MyTextField myTextField) { // 小時文本域 位置:中間 myTextField.getHour().setColumns(4);// 4列 myTextField.getHour().setForeground(Color.blue); // 初始化小時文本域,設置為系統當前小時數 myTextField.getHour().setText( String .valueOf(Calendar.getInstance().get( Calendar.HOUR_OF_DAY))); // 分鍾文本域 位置:中間 myTextField.getMin().setColumns(4);// 4列 myTextField.getMin().setForeground(Color.blue); // 初始化分鍾文本域,設置值為0 myTextField.getMin().setText("0"); // 秒鍾文本域 位置:中間 myTextField.getSec().setColumns(4);// 4列 myTextField.getSec().setForeground(Color.blue); // 初始化秒鍾文本域,設置為系統當前秒鍾數 myTextField.getSec().setText( String.valueOf(Calendar.getInstance().get(Calendar.SECOND))); } /** * 所有的JLabel屬性設置 */ public void setLabel(MyLabel myLabel) { // 現在時間 頭部顯示 myLabel.getTime_now().setForeground(Color.red); myLabel.getTime_now().setFont(new Font("楷體", Font.BOLD, 20)); myLabel.getSurplus().setForeground(Color.red); myLabel.getSurplus().setFont(new Font(null, Font.BOLD, 16)); myLabel.getSurplus().setVisible(false);// 初始化是為不可見 myLabel.getConcept().setForeground(Color.gray); myLabel.getWorkspace().setForeground(Color.gray); } }
/shutdownApp/src/com/b510/examples/window/listener/MyButtonListener.java
1 package com.b510.examples.window.listener; 2 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 6 /** 7 * @author Hongten 8 * 9 * @time 2011-9-5 2011 10 */ 11 public class MyButtonListener implements ActionListener{ 12 13 @Override 14 public void actionPerformed(ActionEvent e) { 15 16 } 17 18 }
源碼下載:http://files.cnblogs.com/hongten/java_shutdownApp.zip
I'm Hongten