首页 > 代码库 > 第4次作业类测试代码+074+林盼皇

第4次作业类测试代码+074+林盼皇

(友情提示:代码部分较多,为了便于测试,项目码源已上传至链接:http://pan.baidu.com/s/1pLscU3T 密码:ug8i)

 界面:

技术分享

 

1、类图

技术分享

2、界面和相应的功能。

本次实验是在原来的CalDate日期计算类的基础上,添加了两个方法int weekDay(int m,int d,int y)与String lastDate(int m,int d,int y),此外还编写了GUInterface界面。

 a、实现lastDate

 1 public  String lastDate(int year, int month, int day)//lastDate方法
 2     {
 3         String lastDate = null;//用来接收日期
 4         checkLeapYear(year);//如果是闰年则将m[2]置为29
 5         switch(checkInput(year,month,day))//判断输入的的合法性属于哪种类型,如果输入合法再进行相关操作
 6         {        
 7             case 1: return lastDate = "月份超出范围"; 
 8             case 2: return lastDate = "日期超出范围"; 
 9             case 3: return lastDate = "年份超出范围"; 
10             default:    
11                 if(month==1 && day==1)//判断是否为每年的第一天,如果 是则年份减一,为12月31日
12                 {
13                     year--;
14                     month = 12;
15                     day = 31;
16                 }
17                 else if(day==1 && month!=1)
18                 {//如果是非1月的第一天,则为上个月的最后一天
19                     month = month-1;
20                     day = m[month];
21                 }
22                 else //如果是其它日期
23                 {
24                     day--;
25                 }
26                 lastDate=year+"年"+month+"月"+day+"日";
27                 m[2] = 28;//重置二月的天数
28                 return lastDate;            
29         }
30     
31     }

b、实现weekDay,利用蔡勒公式技术分享

 1 //weekDay方法,根据蔡勒公式得到对应的日期,返回0表示星期日
 2     public  int weekDay(int year, int month, int day){
 3         if(month==1||month==2)//判断month是否为1或2 
 4         {
 5         year--;
 6         month+=12;
 7         }
 8         int c=year/100;
 9         int y=year-c*100;
10         int week=(c/4)-2*c+(y+y/4)+(13*(month+1)/5)+day-1;
11         while(week<0){week+=7;}
12         week%=7;
13         return week;
14     }

3、整个计算日期类CalDate代码

 1 public class CalDate {
 2     static int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//静态数据存储月份的天数
 3 
 4     //weekDay方法,根据蔡勒公式得到对应的日期,返回0表示星期日
 5     public  int weekDay(int year, int month, int day){
 6         if(month==1||month==2)//判断month是否为1或2 
 7         {
 8             year--;
 9             month+=12;
10         }
11         int c=year/100;
12         int y=year-c*100;
13         int week=(c/4)-2*c+(y+y/4)+(13*(month+1)/5)+day-1;
14         while(week<0){week+=7;}
15         week%=7;
16         return week;
17     }
18     public  String lastDate(int year, int month, int day)//lastDate方法
19     {
20         String lastDate = null;//用来接收日期
21         checkLeapYear(year);//如果是闰年则将m[2]置为29
22         switch(checkInput(year,month,day))//判断输入的的合法性属于哪种类型,如果输入合法再进行相关操作
23         {        
24             case 1: return lastDate = "月份超出范围"; 
25             case 2: return lastDate = "日期超出范围"; 
26             case 3: return lastDate = "年份超出范围"; 
27             default:    
28                 if(month==1 && day==1)//判断是否为每年的第一天,如果 是则年份减一,为12月31日
29                 {
30                     year--;
31                     month = 12;
32                     day = 31;
33                 }
34                 else if(day==1 && month!=1)
35                 {//如果是非1月的第一天,则为上个月的最后一天
36                     month = month-1;
37                     day = m[month];
38                 }
39                 else //如果是其它日期
40                 {
41                     day--;
42                 }
43                 lastDate=year+"年"+month+"月"+day+"日";
44                 m[2] = 28;//重置二月的天数
45                 return lastDate;            
46         }
47     
48     }
49     public  String nextDate(int year,int month,int day)//nextDate方法
50     {
51         String newDate = null;//用来接收日期
52         checkLeapYear(year);//如果是闰年则将m[2]置为29
53         switch(checkInput(year,month,day))//判断输入的的合法性属于哪种类型,如果输入合法再进行相关操作
54         {        
55             case 1: return newDate = "月份超出范围"; 
56             case 2: return newDate = "日期超出范围"; 
57             case 3: return newDate = "年份超出范围"; 
58             default:    
59                 if(month==12 && day==31)//判断是否为最后一个月的最后一天,如果 是则年份加1,为1月1日
60                 {
61                     year++;
62                     month = 1;
63                     day = 1;
64                 }
65                 else if(day < m[month]) day++;//如果日期的天数小于本月天数执行day++;否则表示为当月的最后一天执行month++,day=1;
66                 else 
67                 {
68                     month++;
69                     day =1;
70                 }
71                 newDate=year+"年"+month+"月"+day+"日";
72                 m[2] = 28;//重置二月的天数
73                 return newDate;            
74         }
75     }
76     public  void checkLeapYear(int year)//如果是闰年则将m[2]置为29
77     {
78         if(year%4 == 0 && year%100 != 0)
79             m[2] = 29;//闰年2月29天
80         if(year%400 == 0)
81             m[2] = 29;
82     }
83     public  int checkInput(int year,int month, int day)//判断输入是否合法,并分成不同类型
84     {
85         if(month<1 || month>12)
86             return 1;
87         if(day<1 || day>m[month])
88             return 2;
89         if(year<1992 || year>2050)
90             return 3;
91         return 0;
92     }
93 }

4、图形界面的主要是使用CalDate类中的方法

a、ok按钮的响应事件,调用相应方法将分别将计算结果显示出来

 1 okButton.addActionListener(new ActionListener() {
 2             
 3             @Override
 4             public void actionPerformed(ActionEvent e) {
 5                 if(e.getSource() == okButton){//判断触发源是否是ok按钮
 6                     //获取年月日
 7                     int year = Integer.parseInt( yearText.getText() );
 8                     int month = Integer.parseInt( monthText.getText() );
 9                     int day = Integer.parseInt( dayText.getText() );
10                     
11                     //调用日期计算类的weekDay方法,只有合法输入时才调用
12                     if(calDate.checkInput(year, month, day) == 0)
13                     {
14                         String weekDay = null;
15                         switch(calDate.weekDay(year, month, day)){
16                             case 0: weekDay = "日"; break;
17                             case 1: weekDay = "一"; break;
18                             case 2: weekDay = "二"; break;
19                             case 3: weekDay = "三"; break;
20                             case 4: weekDay = "四"; break;
21                             case 5: weekDay = "五"; break;
22                             case 6: weekDay = "六"; break;
23                         }
24                         weekDayText.setText(weekDay);
25                     }
26                     
27                     //调用日期计算类的lastDate方法
28                     lastDayText.setText( calDate.lastDate(year, month, day) );
29                     
30                     //调用日期计算类的NextDate方法
31                     nextDayText.setText( calDate.nextDate(year, month, day) );
32                     
33                     
34                 }
35                 
36             }
37         });

b、cancel按钮的响应事件,将界面的文本框清空

cancelButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                
                if(e.getSource() == cancelButton){//判断触发是否是cancel按钮,重置文本框
                    
                    yearText.setText("");
                    monthText.setText("");
                    dayText.setText("");
                    weekDayText.setText("");
                    lastDayText.setText("");
                    nextDayText.setText("");
                }
            }
            
        });

c、复写窗口关闭程序,退出时出现提示功能

 1 jFrame.addWindowListener(new WindowAdapter() {
 2             //复写窗口关闭程序
 3             public void windowClosing(WindowEvent e){
 4                 int a = JOptionPane.showConfirmDialog(null, "确定关闭吗?", "温馨提示", 
 5                         JOptionPane.YES_NO_OPTION);
 6                     if (a == 0) {  
 7                        System.exit(0);  //关闭
 8                     }
 9             }
10         });

 

5、图形界面类GUInterface完整代码

  1 public class GUInterface {
  2     //声明一个日期计算类对象
  3     private CalDate calDate;
  4     //声明一个窗体对象
  5     private JFrame jFrame = new JFrame("日期计算程序");
  6     //声明按钮对象
  7     private JButton okButton = new JButton("ok");
  8     private JButton cancelButton = new JButton("cancel");
  9     //声明标签
 10     private JLabel yearLabel = new JLabel("年:");
 11     private JLabel monthLabel = new JLabel("月:");
 12     private JLabel dayLabel = new JLabel("日:");
 13     private JLabel weekDayLabel = new JLabel("这天是星期:");
 14     private JLabel lastDayLabel = new JLabel("上一天是:");    
 15     private JLabel nextDayLabel = new JLabel("下一天是:");    
 16     private JLabel infoLabel = new JLabel("请输入需要计算的年月日");    
 17     
 18     //声明文本域
 19     private JTextField yearText = new JTextField();
 20     private JTextField monthText = new JTextField();
 21     private JTextField dayText = new JTextField();
 22     private JTextField weekDayText = new JTextField();
 23     private JTextField lastDayText = new JTextField();
 24     private JTextField nextDayText = new JTextField();
 25     
 26     
 27     public GUInterface(){
 28         
 29         calDate = new CalDate();
 30         okButton.addActionListener(new ActionListener() {
 31             
 32             @Override
 33             public void actionPerformed(ActionEvent e) {
 34                 if(e.getSource() == okButton){//判断触发源是否是ok按钮
 35                     //获取年月日
 36                     int year = Integer.parseInt( yearText.getText() );
 37                     int month = Integer.parseInt( monthText.getText() );
 38                     int day = Integer.parseInt( dayText.getText() );
 39                     
 40                     //调用日期计算类的weekDay方法,只有合法输入时才调用
 41                     if(calDate.checkInput(year, month, day) == 0)
 42                     {
 43                         String weekDay = null;
 44                         switch(calDate.weekDay(year, month, day)){
 45                             case 0: weekDay = "日"; break;
 46                             case 1: weekDay = "一"; break;
 47                             case 2: weekDay = "二"; break;
 48                             case 3: weekDay = "三"; break;
 49                             case 4: weekDay = "四"; break;
 50                             case 5: weekDay = "五"; break;
 51                             case 6: weekDay = "六"; break;
 52                         }
 53                         weekDayText.setText(weekDay);
 54                     }
 55                     
 56                     //调用日期计算类的lastDate方法
 57                     lastDayText.setText( calDate.lastDate(year, month, day) );
 58                     
 59                     //调用日期计算类的NextDate方法
 60                     nextDayText.setText( calDate.nextDate(year, month, day) );
 61                     
 62                     
 63                 }
 64                 
 65             }
 66         });
 67         cancelButton.addActionListener(new ActionListener(){
 68 
 69             @Override
 70             public void actionPerformed(ActionEvent e) {
 71                 
 72                 if(e.getSource() == cancelButton){//判断触发是否是cancel按钮,重置文本框
 73                     
 74                     yearText.setText("");
 75                     monthText.setText("");
 76                     dayText.setText("");
 77                     weekDayText.setText("");
 78                     lastDayText.setText("");
 79                     nextDayText.setText("");
 80                 }
 81             }
 82             
 83         });
 84         jFrame.addWindowListener(new WindowAdapter() {
 85             //复写窗口关闭程序
 86             public void windowClosing(WindowEvent e){
 87                 int a = JOptionPane.showConfirmDialog(null, "确定关闭吗?", "温馨提示", 
 88                         JOptionPane.YES_NO_OPTION);
 89                     if (a == 0) {  
 90                        System.exit(0);  //关闭
 91                     }
 92             }
 93         });
 94         
 95         jFrame.setLayout(null);
 96         infoLabel.setBounds(5,5,220,30);//设置标签的位置与大小
 97         yearLabel.setBounds(5,35,30,30);
 98         yearText.setBounds(25,35,50,30);
 99         monthLabel.setBounds(75,35,30,30);
100         monthText.setBounds(105,35,30,30);
101         dayLabel.setBounds(135,35,30,30);
102         dayText.setBounds(165,35,30,30);
103         okButton.setBounds(10,70,75,20);//设置按钮的位置与大小
104         cancelButton.setBounds(120,70,75,20);
105         weekDayLabel.setBounds(5,95,100,20);
106         weekDayText.setBounds(110,95,40,20);//设置文本框的位置与大小
107         lastDayLabel.setBounds(5, 125, 100, 20);
108         lastDayText.setBounds(110, 125, 100, 20);
109         nextDayLabel.setBounds(5, 155, 100, 20);
110         nextDayText.setBounds(110, 155, 100, 20);
111         jFrame.add(infoLabel);//向窗体加入标签、文本框、按钮
112         jFrame.add(yearLabel);
113         jFrame.add(yearText);
114         jFrame.add(monthLabel);
115         jFrame.add(monthText);
116         jFrame.add(dayLabel);
117         jFrame.add(dayText);
118         jFrame.add(okButton);
119         jFrame.add(cancelButton);
120         jFrame.add(weekDayLabel);
121         jFrame.add(weekDayText);
122         jFrame.add(lastDayLabel);
123         jFrame.add(lastDayText);
124         jFrame.add(nextDayLabel);
125         jFrame.add(nextDayText);
126         
127         jFrame.setSize(300,250);//设置窗口大小
128         jFrame.setVisible(true);
129     }
130 }

6、最后运行结果

技术分享 技术分享

 

第4次作业类测试代码+074+林盼皇