1、1. import java.awt.*; 2. import java.awt.event.*; 3. import java.text.SimpleDateFormat; 4. import java.util.Calendar; 5. import java.util.Date; 6. import javax.swing.*; 7. 8. /** 9. * 日期计算器 10. * 11. * @author Cyin 12. * @author Lee 13. * @author YOYO 14. * @author Skittles 15. *
2、16. * @since 2008-11-24 17. * @version 0.1 18. * 19. */ 20. public class DateCalculator extends JFrame { 21. 22. /** 23. * 主面板 24. */ 25. JPanel context = new JPanel(); 26. 27. /** 28. * 三个子面板 29. */ 30.
3、 JPanel displayPanel = new JPanel(); 31. JPanel numPanel = new JPanel(); 32. JPanel opPanel = new JPanel(); 33. 34. /** 35. * 结果文本框 36. */ 37. JTextField result = new JTextField("0",20); 38. 39. /** 40. *
4、主要按钮 41. */ 42. JButton btnCE = new JButton("CE"); 43. JButton btnPlus = new JButton("+"); 44. JButton btnMinus = new JButton("-"); 45. JButton btnEqu = new JButton("="); 46. JButton btnX = new JButton("/"); 47. 48. /** 49.
5、 * 是否在输入日期或天数状态的标记 50. */ 51. boolean inputing = false; 52. 53. /** 54. * 是否在进行+-操作的标记 55. */ 56. boolean plus = false; 57. 58. /** 59. * 是否在进行-操作的标记 60. */ 61. boolean minus = fals
6、e; 62. 63. /** 64. * 当前日期 65. */ 66. Calendar calendar = Calendar.getInstance(); 67. 68. /** 69. * 键盘监听器 70. */ 71. KeyboardListener keyListener = new KeyboardListener(); 72. 73. /** 74. * 按钮监听
7、器 75. */ 76. ButtonActionListener btnListener = new ButtonActionListener(); 77. 78. /** 79. * 构造器 80. */ 81. public DateCalculator(){ 82. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 83. this.s
8、etSize(260,220); 84. this.setTitle("日期计算器"); 85. this.setResizable(false); 86. 87. this.setLayout(new FlowLayout()); 88. this.add(context); 89. 90. initWindow(); 91.
9、 92. this.setVisible(true); 93. } 94. 95. /** 96. * 初始化窗体 97. */ 98. private void initWindow(){ 99. context.setLayout(new BorderLayout(10,10)); 100. 101. // 显示面板
10、102. displayPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); 103. displayPanel.add(result); 104. result.setHorizontalAlignment(JTextField.RIGHT); 105. result.setFocusable(false); 106. context.add(displayPanel,"Nor
11、th"); 107. 108. // 数字面板 109. numPanel.setLayout(new GridLayout(4,3,5,5)); 110. for(int i=1;i<=9;i++){ 111. numPanel.add(getNumberButton(i)); 112. } 113. numPanel.add(bt
12、nCE); 114. btnCE.addKeyListener(keyListener); 115. numPanel.add(getNumberButton(0)); 116. 117. btnCE.addActionListener(new ActionListener(){ 118. 119. public void actionPerformed(ActionEvent e) { 120.
13、 inputing = plus = minus = false; 121. result.setText("0"); 122. result.setHorizontalAlignment(JTextField.RIGHT); 123. } 124. 125. });
14、 126. context.add(numPanel,"Center"); 127. 128. // 操作面板 129. opPanel.setSize(50,300); 130. opPanel.setLayout(new GridLayout(4,1,5,5)); 131. opPanel.add(btnPlus); 132. opP
15、anel.add(btnMinus); 133. opPanel.add(btnX); 134. opPanel.add(btnEqu); 135. btnPlus.addActionListener(btnListener); 136. btnPlus.addKeyListener(keyListener); 137. btnMinus.addActionListener(btnListener); 138.
16、 btnMinus.addKeyListener(keyListener); 139. btnX.addActionListener(btnListener); 140. btnX.addKeyListener(keyListener); 141. btnEqu.addActionListener(new ActionListener(){ 142. 143. public void actionPerformed(Ac
17、tionEvent e) { 144. pressEqualButton(); 145. } 146. 147. }); 148. btnEqu.addKeyListener(keyListener); 149. context.add(opPanel,"East"); 150. } 151.
18、 152. /** 153. * 创建数字按钮 154. * @param n 按钮上的数字 155. * @return 创建的按钮 156. */ 157. public JButton getNumberButton(final int n){ 158. JButton btn = new JButton(String.valueOf(n)); 159. btn.setSize(30,50); 160.
19、 btn.addActionListener(btnListener); 161. btn.addKeyListener(keyListener); 162. return btn; 163. } 164. 165. /** 166. * 操作0-9,/按钮或输入0-9,/时调用的方法 167. * @param string 输入的值 168. */ 169. pub
20、lic void pressButton(String string){ 170. if(!inputing){result.setText("");} 171. result.setText(result.getText() + String.valueOf(string)); 172. inputing = true; 173. result.setHorizontalAlignment(JTextField.RIGHT); 174. }
21、 175. 176. /** 177. * 操作+-两个按钮或输入+-时调用的方法。 178. * @param s 输入指令 179. */ 180. private void pressOPButton(String s){ 181. if(plus){ 182. try{ 183. int num = Integer.parse
22、Int(result.getText()); 184. if(minus){ 185. calendar.add(Calendar.DATE, -num); 186. }else{ 187. calendar.add(Calendar.DATE, num); 188.
23、 } 189. minus = false; 190. }catch(Exception e1){ 191. result.setText("天数必须是数字!"); 192. inputing = false; 193. } 194. }
24、else{ 195. if(inputing)saveDate(); 196. } 197. plus = true; 198. if(s.equals("-")){ 199. minus = true; 200. } 201. inputing = false; 202. } 203. 2
25、04. /** 205. * 输入=或按下=时调用的方法 206. */ 207. private void pressEqualButton() { 208. boolean flag = true; 209. if(plus){ 210. try{ 211. int num = Integer.parseInt(result.getTe
26、xt()); 212. if(minus){ 213. calendar.add(Calendar.DATE, -num); 214. }else{ 215. calendar.add(Calendar.DATE, num); 216.
27、} 217. minus = false; 218. }catch(Exception e1){ 219. if(minus){ 220. Calendar cal = this.toDate(); 221. if(cal==null){ 222.
28、 result.setText("请输入天数或日期!"); 223. inputing = false; 224. flag = false; 225. }else{ 226.
29、 calendar.add(Calendar.DATE,-cal.get(Calendar.DATE)); 227. calendar.add(Calendar.MONTH,-(cal.get(Calendar.MONTH)+1)); 228. calendar.add(Calendar.YEAR,-cal.get(Calendar.YEAR)); 229
30、 } 230. }else{ 231. result.setText("天数必须是数字!"); 232. inputing = false; 233. flag = false; 234.
31、 } 235. } 236. plus = false; 237. minus = false; 238. }else{ 239. flag = saveDate(); 240. } 241. if(flag)translate(); 242.
32、 } 243. 244. /** 245. * 储存当前日期 246. * @return 当保存成功时返回true,否则返回false。 247. */ 248. public boolean saveDate(){ 249. Calendar cal = this.toDate(); 250. if(cal==null){ 251. return f
33、alse; 252. } 253. calendar = cal; 254. return true; 255. } 256. 257. /** 258. * 将文本框中的字符串转换为日期 259. */ 260. public Calendar toDate(){ 261. Calendar cal = Calendar.getInstance()
34、 262. String str = result.getText(); 263. SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); 264. try { 265. Date date = format.parse(str); 266. cal.setTime(date); 267.
35、 268. int yy = Integer.parseInt(str.split("/")[0]); 269. int mm = Integer.parseInt(str.split("/")[1]); 270. int dd = Integer.parseInt(str.split("/")[2]); 271. if(!plus&&(yy!=cal.get(Calendar.YEA
36、R)||mm!=cal.get(Calendar.MONTH)+1||dd!=cal.get(Calendar.DAY_OF_MONTH))){ 272. throw new Exception(); 273. } 274. } catch (Exception e) { 275. result.setText("输入的日期有误!"); 276.
37、return null; 277. } 278. return cal; 279. } 280. 281. /** 282. * 获得当前日期是哪个星期 283. */ 284. public void translate(){ 285. result.setHorizontalAlignment(JTextField.LEFT); 286. inp
38、uting = false; 287. SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); 288. try { 289. int i = calendar.get(Calendar.DAY_OF_WEEK); 290. String weekStr[] = { 291.
39、 "", 292. "星期日", 293. "星期一", 294. "星期二", 295.
40、 "星期三", 296. "星期四", 297. "星期五", 298.
41、 "星期六", 299. }; 300. result.setText(format.format(calendar.getTime())+"是" + weekStr[i]); 301. } catch (Exception e) { 302. result.setText("输入的日期格式
42、有误!"); 303. } 304. } 305. 306. /** 307. * 按钮监听器 308. * 309. * @author Cyin 310. * @author Lee 311. * @author YOYO 312. * @author Skittles 313. * 314. * @since 2008-11-24 315
43、 * @version 0.1 316. * 317. */ 318. public class ButtonActionListener implements ActionListener{ 319. 320. public void actionPerformed(ActionEvent e) { 321. if(e.getSource()==btnPlus||e.getSource()==btnMinus){ 322.
44、 pressOPButton(e.getActionCommand()); 323. }else{ 324. pressButton(e.getActionCommand()); 325. } 326. } 327. 328. } 329. 330. /** 331.
45、 * 键盘监听器 332. * 333. * @author Cyin 334. * @author Lee 335. * @author YOYO 336. * @author Skittles 337. * 338. * @since 2008-11-24 339. * @version 0.1 340. * 341. */ 342. public class Keyb
46、oardListener implements KeyListener{ 343. 344. public void keyPressed(KeyEvent e) { 345. } 346. 347. public void keyReleased(KeyEvent e) { 348. } 349. 350. public void keyTyped(
47、KeyEvent e) { 351. char ch = e.getKeyChar(); 352. if(ch>='0'&&ch<='9'||ch=='/'){ 353. pressButton(String.valueOf(ch)); 354. } 355. if(ch=='='){ 356.
48、 pressEqualButton(); 357. } 358. if(ch=='+'||ch=='-'){ 359. pressOPButton(String.valueOf(ch)); 360. } 361. } 362. 363. } 364. 365. 366. /** 367. * 入口方法 368. * @param args 369. */ 370. public static void main(String[] args) { 371. new DateCalculator(); 372. } 373. 374. } 七、 调试与运行情况 输入日期,再输入=或按下=按钮(左图操作,右图结果):
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818