博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第4次作业类测试代码+105032014116+陈洁
阅读量:7033 次
发布时间:2019-06-28

本文共 7761 字,大约阅读时间需要 25 分钟。

1.类图:

2.程序界面:

3.

Date.java

1 package test; 2  3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6  7 public class Date { 8     public String  nextdate(int m,int d,int y){ 9         Calendar calendar = Calendar.getInstance();10         calendar.set(Calendar.YEAR, y);11         calendar.set(Calendar.MONTH, m - 1);12         calendar.set(Calendar.DATE, d + 1);13         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");14         String date=sdf.format(calendar.getTime());15         return date;16     }17     public String lastDay(int m,int d,int y){18         Calendar calendar = Calendar.getInstance();19         calendar.set(Calendar.YEAR, y);20         calendar.set(Calendar.MONTH, m - 1);21         calendar.set(Calendar.DATE, d -1);22         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");23         String date=sdf.format(calendar.getTime());24         return date;25         26     }27     public int weekDay(int m,int d,int y){28         String date=y+"年"+m+"月"+d+"日";29         SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");30         Calendar c = Calendar.getInstance();31         try {32             c.setTime(format.parse(date));33         } catch (ParseException e) {34             // TODO 自动生成的 catch 块35             return 0;//错误提示36         }37         38         int weekday = 0;39         if(c.get(Calendar.DAY_OF_WEEK) == 1){40             weekday = 7;41         }else{42             weekday = c.get(Calendar.DAY_OF_WEEK) - 1;43         }44         return weekday;45     }46     public boolean isDate(String date) {47         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");48         dateFormat.setLenient(false);49         try {50           dateFormat.parse(date.trim());51         }52         catch (ParseException pe) {53           return false;54         }55         return true;56       }57 }

Frame.java

1 package test;  2   3 import java.awt.Font;  4 import java.awt.event.ActionEvent;  5 import java.awt.event.ActionListener;  6 import java.awt.event.WindowAdapter;  7 import java.awt.event.WindowEvent;  8   9 import javax.swing.JButton; 10 import javax.swing.JFrame; 11 import javax.swing.JLabel; 12 import javax.swing.JOptionPane; 13 import javax.swing.JTextField; 14  15 public class Frame { 16      17         Date date=new Date(); 18        19         JFrame frame = new JFrame("日期计算程序"); 20         JLabel title = new JLabel("请输入年月日"); 21         JLabel l_year = new JLabel("年:"); 22         JLabel l_month = new JLabel("月:"); 23         JLabel l_day = new JLabel("日:"); 24         JLabel thisWeek = new JLabel("这一天是星期"); 25         JLabel nextDay = new JLabel("下一天是:"); 26         JLabel lastDay = new JLabel("上一天是:"); 27          28         JTextField t_year = new JTextField(); 29         JTextField t_month = new JTextField(); 30         JTextField t_day = new JTextField(); 31         JTextField t_thisDay = new JTextField(); 32         JTextField t_nextDay = new JTextField(); 33         JTextField t_lastDay = new JTextField(); 34         JButton b_OK = new JButton("OK"); 35         JButton b_Cancel = new JButton("Cancel"); 36         public Frame(){ 37             frame.setLayout(null); 38             Font ft = new Font("微软雅黑", Font.PLAIN, 18);// 设置显示字体 39             title.setFont(ft); 40             title.setBounds(62, 62, 500, 25); 41             l_year.setFont(ft); 42             l_year.setBounds(80, 124, 50, 25); 43             l_month.setFont(ft); 44             l_month.setBounds(230, 124, 50, 25); 45             l_day.setFont(ft); 46             l_day.setBounds(380, 124, 50, 25); 47             thisWeek.setFont(ft); 48             thisWeek.setBounds(62, 248, 200, 25); 49             lastDay.setFont(ft); 50             lastDay.setBounds(62, 372, 150, 25); 51             nextDay.setFont(ft); 52             nextDay.setBounds(62, 310, 150, 25); 53          54          55             b_OK.addActionListener(new ActionListener() { 56                 @Override 57                 public void actionPerformed(ActionEvent e) { 58                     // TODO 自动生成的方法存根 59                     int year=0,month=0,day=0; 60                     try{ 61                         year=Integer.parseInt(t_year.getText()); 62                         month=Integer.parseInt(t_month.getText()); 63                         day=Integer.parseInt(t_day.getText()); 64                     }catch(Exception ex){ 65                          JOptionPane.showMessageDialog(null, "输入有误,请重新输入!"); 66                          return; 67                     } 68                     if(year>2050||year<1912||month>12||month<1||day>31||day<1){ 69                         JOptionPane.showMessageDialog(null, "输入有误,请重新输入!"); 70                             return; 71                     } 72                     if(!date.isDate(year+"年"+month+"月"+day+"日")){ 73                         JOptionPane.showMessageDialog(null, "日期不正确!"); 74                         return; 75                     } 76                     t_nextDay.setText(date.nextdate(month, day, year)); 77                     t_lastDay.setText(date.lastDay(month, day, year)); 78                     if(date.weekDay(month, day, year)!=0){ 79                         t_thisDay.setText(""+date.weekDay(month, day, year)); 80                     } 81                      82                 } 83      84             }); 85             b_Cancel.addActionListener(new ActionListener() { 86      87                 @Override 88                 public void actionPerformed(ActionEvent e) { 89                     // TODO 自动生成的方法存根 90                     91                         t_day.setText(""); 92                         t_month.setText(""); 93                         t_year.setText(""); 94                         t_nextDay.setText(""); 95                         t_lastDay.setText(""); 96                         t_thisDay.setText(""); 97                   //  System.exit(1);             98                         } 99             });100             frame.addWindowListener(new WindowAdapter() {101                 public void windowClosing(WindowEvent e) {102                        System.exit(1);103                  }104              });105             t_year.setBounds(130, 124, 50, 25);106             t_month.setBounds(280, 124, 50, 25);107             t_day.setBounds(430, 124, 50, 25);108             t_thisDay.setBounds(180, 245, 100, 25);109             t_thisDay.setFont(ft);110             t_thisDay.setEnabled(false);111             t_nextDay.setBounds(180, 310, 200, 25);112             t_nextDay.setFont(ft);113             t_nextDay.setEnabled(false);114             t_lastDay.setBounds(180, 372, 200, 25);115             t_lastDay.setFont(ft);116             t_lastDay.setEnabled(false);117     118             b_OK.setBounds(180, 187, 100, 30);119             b_Cancel.setBounds(320, 187, 100, 30);120             121             frame.add(title);122             frame.add(l_year);123             frame.add(l_month);124             frame.add(l_day);125             frame.add(thisWeek);126             frame.add(nextDay);127             frame.add(lastDay);128             frame.add(t_year);129             frame.add(t_month);130             frame.add(t_day);131             frame.add(t_thisDay);132             frame.add(t_nextDay);133             frame.add(t_lastDay);134             frame.add(b_OK);135             frame.add(b_Cancel);136     137             frame.setSize(550, 500);138             frame.setLocation(250, 250);139             frame.setVisible(true);140 141         }142         public static void main(String[] args) {143             new Frame();144         }145 }

 

转载于:https://www.cnblogs.com/trottuer/p/6804017.html

你可能感兴趣的文章
小程序文档
查看>>
QQ分享-定制分享卡片
查看>>
DataTable的用法
查看>>
17_服务器提权
查看>>
Python文件指针与Python函数
查看>>
免费16WiFi被吐槽
查看>>
移动UI自动化-Page Objects Pattern
查看>>
-------分割线------\n FriskyPuppy的图论学习之路!
查看>>
JZ2440 裸机驱动 第13章 LCD控制器(2)
查看>>
连邦IT服务IT用户、厂商和服务商
查看>>
浪潮信息10亿投向云计算
查看>>
SVN分支与主干
查看>>
读书笔记--精通CSS高级Web标准解决方案(二)---CSS基础之CSS选择器
查看>>
NodeJS基础(一)
查看>>
关于IOS的视频录制
查看>>
Git 如何上传文件夹
查看>>
基于React的Tab组件
查看>>
数据库基本操作
查看>>
Struts UI标签的使用
查看>>
个人项目实验报告——记事本
查看>>