您的位置:首页 > 编程语言

第4次作业类测试代码+105032014158+余超勇

2017-05-03 15:20 429 查看
1、类图:



2、界面和相应的功能。

(1)实现lastdate方法

1 public String LastDate(int year, int month, int day)
2     {
3         String lastdate = null;
4         if(month==3)  //2月
5         {
6             if(((year%4==0&&year%100!=0)||year%400==0)) //闰年时的2月
7             {
8                     if(day==1)
9                     {
10                         day=29;
11                         month--;
12                     }
13                     else
14                     {
15                         day--;
16                     }
17             }
18             else
19             {
20                 if(day==1)
21                 {
22                     day=28;
23                     month--;
24                 }
25                 else
26                 {
27                     day--;
28                 }
29             }
30         }
31         else if(month==1)  //12月份
32         {
33             if(day==1)
34             {
35                 year--;
36                 day=31;
37                 month=12;
38             }
39             else
40             {
41                 day--;
42             }
43         }
44         //一般月份
45         else
46         {
47             if(month==5||month==7||month==10||month==12)
48             {
49                 if(day==1)
50                 {
51                     month--;
52                     day=30;
53                 }
54                 else
55                 {
56                     day--;
57                 }
58             }
59             else
60             {
61                 if(day==1)
62                 {
63                     month--;
64                     day=31;
65                 }
66                 else
67                 {
68                     day--;
69                 }
70             }
71         }
72         lastdate =  "" + year + "年" + "" + month + "" + "月" + day + "日"  ;
73         return lastdate;
74     }


(2)实现weekday方法

1 public int weekDay(int year, int month, int day)
2     {
3         int num = 0;
4         //蔡勒公式
5         if(month==1||month==2)
6         {
7             month+=12;
8             year-=1;
9         }
10         int c = year/100;
11         year = year - c * 100;
12         num = (year + year/4 + c/4 - 2*c + 26*(month + 1) / 10 + day-1)%7 ;
13         if(num==0)
14         {
15             num+=7;
16         }
17         return num;
18     }


(3)OK按钮实现功能

1 JButton btnNewButton = new JButton("OK");
2         btnNewButton.addActionListener(new ActionListener() {
3             public void actionPerformed(ActionEvent e) {
4                 Date date = new Date();
5                 String t_y = textField.getText();
6                 String t_m = textField_1.getText();
7                 String t_d = textField_2.getText();
8
9                 int year = Integer.parseInt(t_y);
10                 int month = Integer.parseInt(t_m);
11                 int day = Integer.parseInt(t_d);
12                 String msg1 = date.NextDate(year,month,day);
13                 String msg2 = date.LastDate(year,month,day);
14                 int weeki = date.weekDay(year,month,day);
15                 String msg = null;
16                 msg = date.Check(year, month, day);
17                 if(msg!=null)
18                 {
19                     JOptionPane.showMessageDialog(null,  msg, "警告!", JOptionPane.ERROR_MESSAGE);
20                     textField.setText("");
21                     textField_1.setText("");
22                     textField_2.setText("");
23                     textField_3.setText("");
24                     textField_4.setText("");
25                     textField_5.setText("");
26                 }
27                 else
28                 {
29                     textField_4.setText(msg1);
30                     textField_5.setText(msg2);
31                     textField_3.setText(String.valueOf(weeki));
32                 }
33             }
34         }
35         );
36
37         btnNewButton.setBounds(97, 158, 113, 27);
38         getContentPane().add(btnNewButton);


(4)限制输入只能是数字

1 textField.addKeyListener(new KeyAdapter(){
2             public void keyTyped(KeyEvent e) {
3                 int keyChar = e.getKeyChar();
4                 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){
5
6                 }else{
7                     e.consume(); //关键,屏蔽掉非法输入
8                 }
9             }
10         });
11
12         textField_1.addKeyListener(new KeyAdapter(){
13             public void keyTyped(KeyEvent e) {
14                 int keyChar = e.getKeyChar();
15                 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){
16
17                 }else{
18                     e.consume(); //关键,屏蔽掉非法输入
19                 }
20             }
21         });
22
23         textField_2.addKeyListener(new KeyAdapter(){
24             public void keyTyped(KeyEvent e) {
25                 int keyChar = e.getKeyChar();
26                 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){
27
28                 }else{
29                     e.consume(); //关键,屏蔽掉非法输入
30                 }
31             }
32         });


(5)Cancle按钮的功能

1 JButton btnNewButton_1 = new JButton("Cancle");
2         btnNewButton_1.addActionListener(new ActionListener() {
3             public void actionPerformed(ActionEvent e) {
4                 textField.setText("");
5                 textField_1.setText("");
6                 textField_2.setText("");
7                 textField_3.setText("");
8                 textField_4.setText("");
9                 textField_5.setText("");
10             }
11         });
12         btnNewButton_1.setBounds(331, 158, 113, 27);
13         getContentPane().add(btnNewButton_1);


(6)全部代码:

功能:

1 public class Date {
2     public String Check(int year, int month, int day)
3     {
4         String msg = null;
5         if((year<1912||year>2050))  //判断年份是否超出
6         {
7             msg = "输入有误,请重新输入!";
8             return msg;
9         }
10         if((month<1||month>12))  // 判断月份是否超出
11         {
12             msg += "输入有误,请重新输入!";
13             return msg;
14         }
15         //判断日期是否超出
16         if(month==4||month==6||month==9||month==11)
17         {
18             if((day<1||day>30))
19             {
20                 msg += "输入有误,请重新输入!";
21                 return msg;
22             }
23         }
24         else if(month==2)
25         {
26             if((year%4==0&&year%100!=0)||year%400==0)//闰年
27             {
28                 if((day<1||day>29))
29                 {
30                     msg += "输入有误,请重新输入!";
31                     return msg;
32                 }
33             }
34             else      //非闰年
35             {
36                 if((day<1||day>28))
37                 {
38                     msg += "输入有误,请重新输入!";
39                     return msg;
40                 }
41             }
42         }
43         else
44         {
45             if((day<1||day>31))
46             {
47                 msg += "输入有误,请重新输入!";
48                 return msg;
49             }
50         }
51         return msg;
52     }
53
54     public String NextDate(int year, int month, int day)
55     {
56
57         String nextdate;
58         if(month==2)  //2月
59         {
60             if(((year%4==0&&year%100!=0)||year%400==0)) //闰年时的2月
61             {
62                     if(day==29)
63                     {
64                         day=1;
65                         month++;
66                     }
67                     else
68                     {
69                         day++;
70                     }
71             }
72             else
73             {
74                 if(day==28)
75                 {
76                     day=1;
77                     month++;
78                 }
79                 else
80                 {
81                     day++;
82                 }
83             }
84         }
85         else if(month==12)  //12月份
86         {
87             if(day==31)
88             {
89                 year++;
90                 day=1;
91                 month=1;
92             }
93             else
94             {
95                 day++;
96             }
97         }
98         //一般月份
99         else
100         {
101             if(month==4||month==6||month==9||month==11)
102             {
103                 if(day==30)
104                 {
105                     month++;
106                     day=1;
107                 }
108                 else
109                 {
110                     day++;
111                 }
112             }
113             else
114             {
115                 if(day==31)
116                 {
117                     month++;
118                     day=1;
119                 }
120                 else
121                 {
122                     day++;
123                 }
124             }
125         }
126         nextdate =  "" + year + "年" + "" + month + "" + "月" + day + "日"  ;
127         return nextdate;
128     }
129
130     public String LastDate(int year, int month, int day)
131     {
132         String lastdate = null;
133         if(month==3)  //2月
134         {
135             if(((year%4==0&&year%100!=0)||year%400==0)) //闰年时的2月
136             {
137                     if(day==1)
138                     {
139                         day=29;
140                         month--;
141                     }
142                     else
143                     {
144                         day--;
145                     }
146             }
147             else
148             {
149                 if(day==1)
150                 {
151                     day=28;
152                     month--;
153                 }
154                 else
155                 {
156                     day--;
157                 }
158             }
159         }
160         else if(month==1)  //12月份
161         {
162             if(day==1)
163             {
164                 year--;
165                 day=31;
166                 month=12;
167             }
168             else
169             {
170                 day--;
171             }
172         }
173         //一般月份
174         else
175         {
176             if(month==5||month==7||month==10||month==12)
177             {
178                 if(day==1)
179                 {
180                     month--;
181                     day=30;
182                 }
183                 else
184                 {
185                     day--;
186                 }
187             }
188             else
189             {
190                 if(day==1)
191                 {
192                     month--;
193                     day=31;
194                 }
195                 else
196                 {
197                     day--;
198                 }
199             }
200         }
201         lastdate =  "" + year + "年" + "" + month + "" + "月" + day + "日"  ;
202         return lastdate;
203     }
204
205     public int weekDay(int year, int month, int day)
206     {
207         int num = 0;
208         //蔡勒公式
209         if(month==1||month==2)
210         {
211             month+=12;
212             year-=1;
213         }
214         int c = year/100;
215         year = year - c * 100;
216         num = (year + year/4 + c/4 - 2*c + 26*(month + 1) / 10 + day-1)%7 ;
217         if(num==0)
218         {
219             num+=7;
220         }
221         return num;
222     }
223 }


界面:

1 import java.awt.EventQueue;
2 import java.awt.event.ActionEvent;
3 import java.awt.event.ActionListener;
4 import java.awt.event.KeyAdapter;
5 import java.awt.event.KeyEvent;
6
7 import javax.swing.JButton;
8 import javax.swing.JFrame;
9
10 import javax.swing.JLabel;
11 import javax.swing.JOptionPane;
12 import javax.swing.JTextField;
13
14 public class ruance_1 extends JFrame {
15     private JTextField textField;
16     private JTextField textField_1;
17     private JTextField textField_2;
18     private JTextField textField_3;
19     private JTextField textField_4;
20     private JTextField textField_5;
21     public ruance_1() {
22         setTitle("\u65E5\u671F\u8BA1\u7B97\u7A0B\u5E8F");
23         getContentPane().setLayout(null);
24
25         JLabel lblNewLabel = new JLabel("\u8BF7\u8F93\u5165\u9700\u8981\u8BA1\u7B97\u7684\u5E74\u6708\u65E5(\u6700\u597D\u662F\u6570\u5B57)\uFF1A");
26         lblNewLabel.setBounds(88, 31, 287, 18);
27         getContentPane().add(lblNewLabel);
28
29         JLabel lblNewLabel_1 = new JLabel("\u5E74\uFF1A");
30         lblNewLabel_1.setBounds(14, 79, 30, 18);
31         getContentPane().add(lblNewLabel_1);
32
33         textField = new JTextField();
34         textField.setBounds(44, 76, 86, 24);
35         getContentPane().add(textField);
36         textField.setColumns(10);
37
38         JLabel lblNewLabel_2 = new JLabel("\u6708\uFF1A");
39         lblNewLabel_2.setBounds(193, 79, 30, 18);
40         getContentPane().add(lblNewLabel_2);
41
42         textField_1 = new JTextField();
43         textField_1.setBounds(224, 76, 86, 24);
44         getContentPane().add(textField_1);
45         textField_1.setColumns(10);
46
47         JLabel lblNewLabel_3 = new JLabel("\u65E5\uFF1A");
48         lblNewLabel_3.setBounds(372, 79, 30, 18);
49         getContentPane().add(lblNewLabel_3);
50
51         textField_2 = new JTextField();
52         textField_2.setBounds(402, 76, 86, 24);
53         getContentPane().add(textField_2);
54         textField_2.setColumns(10);
55
56         textField.addKeyListener(new KeyAdapter(){
57             public void keyTyped(KeyEvent e) {
58                 int keyChar = e.getKeyChar();
59                 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){
60
61                 }else{
62                     e.consume(); //关键,屏蔽掉非法输入
63                 }
64             }
65         });
66
67         textField_1.addKeyListener(new KeyAdapter(){
68             public void keyTyped(KeyEvent e) {
69                 int keyChar = e.getKeyChar();
70                 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){
71
72                 }else{
73                     e.consume(); //关键,屏蔽掉非法输入
74                 }
75             }
76         });
77
78         textField_2.addKeyListener(new KeyAdapter(){
79             public void keyTyped(KeyEvent e) {
80                 int keyChar = e.getKeyChar();
81                 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){
82
83                 }else{
84                     e.consume(); //关键,屏蔽掉非法输入
85                 }
86             }
87         });
88
89         JButton btnNewButton = new JButton("OK");
90         btnNewButton.addActionListener(new ActionListener() {
91             //判断字符串是否为数字
92 //            public boolean isInteger(String value) {
93 //                  try {
94 //                   Integer.parseInt(value);
95 //                   return true;
96 //                  } catch (NumberFormatException e) {
97 //                   return false;
98 //                  }
99 //                  }
100             public void actionPerformed(ActionEvent e) {
101                 Date date = new Date();
102                 String t_y = textField.getText();
103                 String t_m = textField_1.getText();
104                 String t_d = textField_2.getText();
105
106                 int year = Integer.parseInt(t_y);
107                 int month = Integer.parseInt(t_m);
108                 int day = Integer.parseInt(t_d);
109                 String msg1 = date.NextDate(year,month,day);
110                 String msg2 = date.LastDate(year,month,day);
111                 int weeki = date.weekDay(year,month,day);
112                 String msg = null;
113                 msg = date.Check(year, month, day);
114                 if(msg!="")
115                 {
116                     JOptionPane.showMessageDialog(null,  msg, "警告!", JOptionPane.ERROR_MESSAGE);
117                     textField.setText("");
118                     textField_1.setText("");
119                     textField_2.setText("");
120                     textField_3.setText("");
121                     textField_4.setText("");
122                     textField_5.setText("");
123                 }
124                 else
125                 {
126                     textField_4.setText(msg1);
127                     textField_5.setText(msg2);
128                     textField_3.setText(String.valueOf(weeki));
129                 }
130             }
131         }
132         );
133
134         btnNewButton.setBounds(97, 158, 113, 27);
135         getContentPane().add(btnNewButton);
136
137         JButton btnNewButton_1 = new JButton("Cancle");
138         btnNewButton_1.addActionListener(new ActionListener() {
139             public void actionPerformed(ActionEvent e) {
140                 textField.setText("");
141                 textField_1.setText("");
142                 textField_2.setText("");
143                 textField_3.setText("");
144                 textField_4.setText("");
145                 textField_5.setText("");
146             }
147         });
148         btnNewButton_1.setBounds(331, 158, 113, 27);
149         getContentPane().add(btnNewButton_1);
150
151         JLabel label = new JLabel("\u8FD9\u5929\u662F\u661F\u671F\uFF1A");
152         label.setBounds(92, 261, 118, 18);
153         getContentPane().add(label);
154
155         JLabel label_1 = new JLabel("\u4E0B\u4E00\u5929\u662F\uFF1A");
156         label_1.setBounds(97, 314, 118, 18);
157         getContentPane().add(label_1);
158
159         JLabel label_2 = new JLabel("\u4E0A\u4E00\u5929\u662F\uFF1A");
160         label_2.setBounds(97, 364, 129, 18);
161         getContentPane().add(label_2);
162
163         textField_3 = new JTextField();
164         textField_3.setColumns(10);
165         textField_3.setBounds(193, 258, 86, 24);
166         getContentPane().add(textField_3);
167
168         textField_4 = new JTextField();
169         textField_4.setColumns(10);
170         textField_4.setBounds(193, 311, 168, 24);
171         getContentPane().add(textField_4);
172
173         textField_5 = new JTextField();
174         textField_5.setColumns(10);
175         textField_5.setBounds(193, 361, 168, 24);
176         getContentPane().add(textField_5);
177         this.setVisible(true);
178         this.setSize(619, 503);
179         this.setLocation(300, 200);
180     }
181
182     public static void main(String[] args) {
183         EventQueue.invokeLater(new Runnable() {
184             public void run() {
185                 try {
186                     ruance_1 frame = new ruance_1();
187                     frame.setVisible(true);
188                 } catch (Exception e) {
189                     e.printStackTrace();
190                 }
191             }
192         });
193     }
194 }


3、效果图:







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: