收藏 分销(赏)

日期计算器java.doc

上传人:仙人****88 文档编号:9378986 上传时间:2025-03-24 格式:DOC 页数:11 大小:188.50KB
下载 相关 举报
日期计算器java.doc_第1页
第1页 / 共11页
日期计算器java.doc_第2页
第2页 / 共11页
点击查看更多>>
资源描述
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. * 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.         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.          * 主要按钮 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.          * 是否在输入日期或天数状态的标记 50.          */ 51.         boolean inputing = false; 52.         53.         /** 54.          * 是否在进行+-操作的标记 55.          */ 56.         boolean plus = false; 57.         58.         /** 59.          * 是否在进行-操作的标记 60.          */ 61.         boolean minus = false; 62.         63.         /** 64.          * 当前日期 65.          */ 66.         Calendar calendar = Calendar.getInstance(); 67.   68.         /** 69.         * 键盘监听器 70.         */ 71.         KeyboardListener keyListener = new KeyboardListener(); 72.   73.         /** 74.         * 按钮监听器 75.         */ 76.         ButtonActionListener btnListener = new ButtonActionListener(); 77.         78.         /** 79.          * 构造器 80.          */ 81.         public DateCalculator(){ 82.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 83.                 this.setSize(260,220); 84.                 this.setTitle("日期计算器");        85.                 this.setResizable(false); 86.                 87.                 this.setLayout(new FlowLayout()); 88.                 this.add(context); 89.                 90.                 initWindow(); 91.                 92.                 this.setVisible(true); 93.         } 94.         95.         /** 96.          * 初始化窗体 97.          */ 98.         private void initWindow(){ 99.                 context.setLayout(new BorderLayout(10,10)); 100.                 101.                 //      显示面板 102.                 displayPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); 103.                 displayPanel.add(result); 104.                 result.setHorizontalAlignment(JTextField.RIGHT); 105.                 result.setFocusable(false); 106.                 context.add(displayPanel,"North"); 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(btnCE); 114.                 btnCE.addKeyListener(keyListener); 115.                 numPanel.add(getNumberButton(0)); 116.                 117.                 btnCE.addActionListener(new ActionListener(){ 118.   119.                         public void actionPerformed(ActionEvent e) { 120.                                 inputing = plus = minus = false; 121.                                 result.setText("0"); 122.                                 result.setHorizontalAlignment(JTextField.RIGHT); 123.                         } 124.                         125.                 });           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.                 opPanel.add(btnMinus); 133.                 opPanel.add(btnX); 134.                 opPanel.add(btnEqu); 135.                 btnPlus.addActionListener(btnListener); 136.                 btnPlus.addKeyListener(keyListener); 137.                 btnMinus.addActionListener(btnListener); 138.                 btnMinus.addKeyListener(keyListener); 139.                 btnX.addActionListener(btnListener); 140.                 btnX.addKeyListener(keyListener); 141.                 btnEqu.addActionListener(new ActionListener(){ 142.   143.                         public void actionPerformed(ActionEvent e) { 144.                                 pressEqualButton(); 145.                         } 146.                         147.                 }); 148.                 btnEqu.addKeyListener(keyListener); 149.                 context.add(opPanel,"East"); 150.         } 151.         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.                 btn.addActionListener(btnListener); 161.                 btn.addKeyListener(keyListener); 162.                 return btn; 163.         } 164.         165.         /** 166.          * 操作0-9,/按钮或输入0-9,/时调用的方法 167.          * @param string 输入的值 168.          */ 169.         public 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.         } 175.         176.         /** 177.          * 操作+-两个按钮或输入+-时调用的方法。 178.          * @param s 输入指令 179.          */ 180.         private void pressOPButton(String s){ 181.                 if(plus){ 182.                         try{ 183.                                 int num = Integer.parseInt(result.getText()); 184.                                 if(minus){ 185.                                         calendar.add(Calendar.DATE, -num); 186.                                 }else{ 187.                                         calendar.add(Calendar.DATE, num); 188.                                 } 189.                                 minus = false; 190.                         }catch(Exception e1){ 191.                                 result.setText("天数必须是数字!"); 192.                                 inputing = false; 193.                         } 194.                 }else{ 195.                         if(inputing)saveDate(); 196.                 } 197.                 plus = true; 198.                 if(s.equals("-")){ 199.                         minus = true; 200.                 } 201.                 inputing = false; 202.         } 203.         204.         /** 205.          * 输入=或按下=时调用的方法 206.          */ 207.         private void pressEqualButton() { 208.                 boolean flag = true; 209.                 if(plus){ 210.                         try{ 211.                                 int num = Integer.parseInt(result.getText()); 212.                                 if(minus){ 213.                                         calendar.add(Calendar.DATE, -num); 214.                                 }else{ 215.                                         calendar.add(Calendar.DATE, num); 216.                                 } 217.                                 minus = false; 218.                         }catch(Exception e1){ 219.                                 if(minus){ 220.                                         Calendar cal = this.toDate(); 221.                                         if(cal==null){ 222.                                                 result.setText("请输入天数或日期!"); 223.                                                 inputing = false; 224.                                                 flag = false; 225.                                         }else{ 226.                                                 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.                                         } 230.                                 }else{ 231.                                         result.setText("天数必须是数字!"); 232.                                         inputing = false; 233.                                         flag = false; 234.                                 } 235.                         } 236.                         plus = false; 237.                         minus = false; 238.                 }else{ 239.                         flag = saveDate(); 240.                 } 241.                 if(flag)translate(); 242.         } 243.         244.         /** 245.          * 储存当前日期 246.          * @return 当保存成功时返回true,否则返回false。 247.          */ 248.         public boolean saveDate(){ 249.                 Calendar cal = this.toDate(); 250.                 if(cal==null){ 251.                         return false; 252.                 } 253.                 calendar = cal; 254.                 return true; 255.         } 256.         257.         /** 258.          * 将文本框中的字符串转换为日期 259.          */ 260.         public Calendar toDate(){ 261.                 Calendar cal = Calendar.getInstance(); 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.                         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.YEAR)||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.                         return null; 277.                 } 278.                 return cal; 279.         } 280.         281.         /** 282.          * 获得当前日期是哪个星期 283.          */ 284.         public void translate(){ 285.                 result.setHorizontalAlignment(JTextField.LEFT); 286.                 inputing = false; 287.                 SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); 288.                 try { 289.                         int i = calendar.get(Calendar.DAY_OF_WEEK); 290.                         String weekStr[] = { 291.                                                                         "", 292.                                                                         "星期日", 293.                                                                         "星期一", 294.                                                                         "星期二", 295.                                                                         "星期三", 296.                                                                         "星期四", 297.                                                                         "星期五", 298.                                                                         "星期六", 299.                                                                 }; 300.                         result.setText(format.format(calendar.getTime())+"是" + weekStr[i]); 301.                 } catch (Exception e) { 302.                         result.setText("输入的日期格式有误!"); 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.          * @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.                                 pressOPButton(e.getActionCommand()); 323.                         }else{ 324.                                 pressButton(e.getActionCommand()); 325.                         } 326.                 } 327.   328.         } 329.   330.         /** 331.          * 键盘监听器 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 KeyboardListener implements KeyListener{ 343.                 344.                 public void keyPressed(KeyEvent e) { 345.                 } 346.         347.                 public void keyReleased(KeyEvent e) {    348.                 } 349.         350.                 public void keyTyped(KeyEvent e) { 351.                         char ch = e.getKeyChar(); 352.                         if(ch>='0'&&ch<='9'||ch=='/'){ 353.                                 pressButton(String.valueOf(ch)); 354.                         } 355.                         if(ch=='='){ 356.                                 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. } 七、 调试与运行情况 输入日期,再输入=或按下=按钮(左图操作,右图结果):    
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服