基于Java swing实现的小计划日历

前言

本篇并非技术贴,只是为有需要的人提供参考,我所写的代码有很多限制,例如
只能操作指定格式的计划,删除多行内容会产生一些显示错误等等,期望大家使用
时可以进行优化,本篇只提供一个参考。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JTextField;




public class calendar extends JFrame implements ActionListener{
//月份和年份下拉 列表框
private JComboBox MonthBox = new JComboBox();
private JComboBox YearBox = new JComboBox();

//年份月份标签
private JLabel YearLabel = new JLabel("年份:");
private JLabel MonthLabel = new JLabel("月份:");

//确定和今天按钮
private JButton button_ok = new JButton("查看");
private JButton button_today = new JButton("今天");


//获取今天的日期、年份和月份
private Date now_date = new Date();

//获取当前的日期
private Date current_date = null;

@SuppressWarnings("deprecation")
private int now_year = now_date.getYear() + 1900;
@SuppressWarnings("deprecation")
private int now_month = now_date.getMonth();
private boolean todayFlag = false;

//用一组按钮显示日期,一共7行7列。第一行是星期
private JButton[] button_day = new JButton[42];
private final String[] week = {"SUN","MON","TUE","WEN","THR","FRI","SAT"};
private JButton[] button_week = new JButton[7];
private String year_int = null;
private int month_int;

//设置计划区三个按钮
private JButton button_add = new JButton("添加");
private JButton button_delete = new JButton("删除");
private JButton button_update = new JButton("更新");

//计划区显示与输入
private JTextArea plan_reveal = new JTextArea("");
private JTextField plan_do = new JTextField(30);

/*构造函数*/
public calendar(){
super();
this.setTitle("计划日历");
this.init();
this.setLocation(500, 300);
this.setResizable(false);
pack();

}

//初始化日历
private void init() {
Font font = new Font("Dialog",Font.BOLD,16);
YearLabel.setFont(font);
MonthLabel.setFont(font);
button_ok.setFont(font);
button_today.setFont(font);
button_add.setFont(font);
button_delete.setFont(font);
button_update.setFont(font);

//显示区设置大小
plan_reveal.setLineWrap(true);
plan_reveal.setEditable(false);
plan_reveal.setPreferredSize(new Dimension(30,180));
button_add.setPreferredSize(new Dimension(110,20));
button_delete.setPreferredSize(new Dimension(50,20));
button_update.setPreferredSize(new Dimension(110,20));

//过去20年--未来20年
for(int i = now_year - 20;i <= now_year + 100;i++){
YearBox.addItem(i+"");
}
YearBox.setSelectedIndex(20);

for(int i = 1;i < 13;i++){
MonthBox.addItem(i+"");
}
MonthBox.setSelectedIndex(now_month);

//放置下拉列表框和控制按钮的面板
JPanel panel_ym = new JPanel();
panel_ym.add(YearLabel);
panel_ym.add(YearBox);
panel_ym.add(MonthLabel);
panel_ym.add(MonthBox);
panel_ym.add(button_ok);
panel_ym.add(button_today);

//为两个按钮添加时间监听器
button_ok.addActionListener(this);
button_today.addActionListener(this);
button_add.addActionListener(this);
button_delete.addActionListener(this);
button_update.addActionListener(this);




JPanel panel_day = new JPanel();
//7*7
panel_day.setLayout(new GridLayout(7, 7, 3, 3));
for(int i = 0; i < 7; i++) {
button_week[i] = new JButton(" ");
button_week[i].setText(week[i]);
button_week[i].setForeground(Color.black);
panel_day.add(button_week[i]);
}
button_week[0].setForeground(Color.red);
button_week[6].setForeground(Color.red);

for(int i = 0; i < 42;i++){
button_day[i] = new JButton(" ");
button_day[i].addActionListener(this);
panel_day.add(button_day[i]);
}

this.paintDay();//显示当前日期

//将按钮整合
JPanel panel_do = new JPanel();
panel_do.setLayout(new BorderLayout());
panel_do.add(button_add,BorderLayout.WEST);
panel_do.add(button_delete,BorderLayout.CENTER);
panel_do.add(button_update,BorderLayout.EAST);


//为计划添加面板
JPanel panel_plan = new JPanel();
//加入显示 操作 和按钮
panel_plan.setLayout(new BorderLayout());
panel_plan.add(plan_reveal,BorderLayout.NORTH);
panel_plan.add(plan_do,BorderLayout.CENTER);
panel_plan.add(panel_do,BorderLayout.SOUTH);

JPanel panel_main = new JPanel();
panel_main.setLayout(new BorderLayout());
panel_main.add(panel_day,BorderLayout.CENTER);
panel_main.add(panel_ym,BorderLayout.NORTH);
panel_main.add(panel_plan,BorderLayout.EAST);
getContentPane().add(panel_main);

}

@SuppressWarnings("deprecation")
private void paintDay() {
if(todayFlag){
year_int = now_year +"";
month_int = now_month;
}else{
year_int = YearBox.getSelectedItem().toString();
month_int = MonthBox.getSelectedIndex();
}
int year_sel = Integer.parseInt(year_int) - 1900;
Date firstDay = new Date(year_sel, month_int, 1);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(firstDay);
int days = 0;
int day_week = 0;

if(month_int == 0||month_int == 2||month_int == 4||month_int == 6
||month_int == 7||month_int == 9||month_int == 11){
days = 31;
}else if(month_int == 3||month_int == 5||month_int == 8||month_int == 10){
days = 30;
}else{
if(cal.isLeapYear(year_sel)){
days = 29;
}else{
days = 28;
}
}

day_week = firstDay.getDay();
int count = 1;

for(int i = day_week;i<day_week+days;count++,i++){
if(i%7 == 0||(i+1)%7 == 0){
if((i == day_week+now_date.getDate()-1)&& month_int==now_month && (year_sel == now_year-1900)){
button_day[i].setForeground(Color.BLUE);
//button_day[i].setText(count+"");
button_day[i].setLabel(count+"");
}else{
button_day[i].setForeground(Color.RED);
//button_day[i].setText(count+"");
button_day[i].setLabel(count+"");
}
}else{
if((i == day_week+now_date.getDate()-1)&& month_int==now_month && (year_sel == now_year-1900)){
button_day[i].setForeground(Color.BLUE);
//button_day[i].setText(count+"");
button_day[i].setLabel(count+"");
}else{
button_day[i].setForeground(Color.BLACK);
//button_day[i].setText(count+"");
button_day[i].setLabel(count+"");
}
}

}
if(day_week == 0){
for(int i = days;i<42;i++){
button_day[i].setText("");
}
}else{
for(int i = 0;i<day_week;i++){
button_day[i].setText("");
}
for(int i=day_week+days;i<42;i++){
button_day[i].setText("");
}
}


}



@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button_ok){
todayFlag=false;
this.paintDay();
}
else if(e.getSource()==button_today){
todayFlag=true;
YearBox.setSelectedIndex(20);
MonthBox.setSelectedIndex(now_month);
this.paintDay();
}
//添加
else if (e.getSource()==button_add) {
String ddate = String.valueOf(current_date.getYear())+String.valueOf(current_date.getMonth())+String.valueOf(current_date.getDate());
//System.out.print(ddate);
File file = new File(ddate+".txt");
if(file.exists()) {
try {
FileWriter writer = new FileWriter(file,true);
writer.write(plan_do.getText()+"\n");
writer.flush();
writer.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
else {
try {
file.createNewFile();
FileWriter writer = new FileWriter(file,true);
writer.write(plan_do.getText()+"\n");
writer.flush();
writer.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}


}
//删除
else if (e.getSource()==button_delete) {
String ddate = String.valueOf(current_date.getYear())+String.valueOf(current_date.getMonth())+String.valueOf(current_date.getDate());
//System.out.print(ddate);
File file = new File(ddate+".txt");
if(file.exists()) {
try {
FileReader fr = new FileReader(file);
//前期准备,用char数组操作
char[] a = new char[2000];
fr.read(a);
char[] b = new char[2000];
String d = null;
d = plan_do.getText();
b = d.toCharArray();
//for(int i=0;i<b.length;i++) {System.out.println(b[i]);}
//新建result
char[] result = new char[2000];
//开始筛选
int j=0;
for(int i=0;i<a.length;i++) {
int key = 0;
int count=0;
if((a[i]==b[0]&&i==0)||(a[i]==b[0]&&a[i-1]=='\n')){
//System.out.println("aifind");
for(int ii=i,jj=0;b[jj]!=',';ii++,jj++) {
if(a[ii]==b[jj]) {
key = 1;
}
else {
key = 0;
break;
}}
}
if(key==0) {
result[j] = a[i];
j++;
}
else if(key==1) {
count = 0;
for(int ii=i;a[ii]!='\n';ii++) {
count++;
}
i=i+count;
}
}
//for(int i=0;i<result.length;i++) {System.out.println(result[i]);}

//写入
FileWriter writer = new FileWriter(file);
String reString = String.valueOf(result);
//System.out.println(reString);
writer.write(reString);
writer.flush();
writer.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
else {

}
}
//更新
else if (e.getSource()==button_update) {
String ddate = String.valueOf(current_date.getYear())+String.valueOf(current_date.getMonth())+String.valueOf(current_date.getDate());
//System.out.print(ddate);
File file = new File(ddate+".txt");
if(file.exists()) {
try {
FileReader fr = new FileReader(file);
//前期准备,用char数组操作
char[] a = new char[2000];
fr.read(a);
char[] b = new char[2000];
String d = null;
d = plan_do.getText();
b = d.toCharArray();
//for(int i=0;i<b.length;i++) {System.out.println(b[i]);}
//新建result
char[] result = new char[2000];
//开始筛选
int j=0;
for(int i=0;i<a.length;i++) {
int key = 0;
int count=0;
if((a[i]==b[0]&&i==0)||(a[i]==b[0]&&a[i-1]=='\n')){
//System.out.println("aifind");
for(int ii=i,jj=0;b[jj]!=',';ii++,jj++) {
if(a[ii]==b[jj]) {
key = 1;
}
else {
key = 0;
break;
}}
}
if(key==0) {
result[j] = a[i];
j++;
}
else if(key==1) {
count = 0;
for(int ii=0;b[ii]!='#';ii++) {
result[j] = b[ii];
j++;
count++;
}
i=i+count-1;
}
}
//for(int i=0;i<result.length;i++) {System.out.println(result[i]);}

//写入
FileWriter writer = new FileWriter(file);
String reString = String.valueOf(result);
//System.out.println(reString);
writer.write(reString);
writer.flush();
writer.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
else {

}
}
//显示
else {
for(int i=0;i<42;i++) {
if(e.getSource().equals(button_day[i])) {
if(todayFlag){
year_int = now_year +"";
month_int = now_month;
}else{
year_int = YearBox.getSelectedItem().toString();
month_int = MonthBox.getSelectedIndex();
}
int year_sel = Integer.parseInt(year_int) - 1900;
int dday = Integer.parseInt(button_day[i].getLabel());
current_date = new Date(year_sel, month_int,dday);
}
}
String ddate = String.valueOf(current_date.getYear())+String.valueOf(current_date.getMonth())+String.valueOf(current_date.getDate());
//System.out.print(ddate);
File file = new File(ddate+".txt");
if(file.exists()) {
try {
FileReader fr = new FileReader(file);
char[] a = new char[5000];
fr.read(a);
plan_reveal.setText(String.valueOf(a));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
else {
plan_reveal.setText("暂无内容");
}


}


}

public static void main(String[] args) {
calendar ct = new calendar();
ct.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ct.setVisible(true);

}

}

代码中的限制包括只能读取规定大小内的char数组(或许能用流式处理改进),显示区域无滑动条
以及前言提到的等等。


基于Java swing实现的小计划日历
http://silencezheng.top/2020/07/22/article2/
作者
SilenceZheng66
发布于
2020年7月22日
许可协议