import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.awt.SplashScreen;
public class CalendarWindow extends JFrame implements ActionListener,
MouseListener, FocusListener {
int year, month, day;
CalendarMessage calendarMessage;
CalendarPad calendarPad;
NotePad notePad;
JTextField showYear, showMonth;
JTextField showDay[];
CalendarImage calendarImage;
String picturename;//图像名称;
String getPicture_address;
Clock clock;
JButton nextYear, previousYear, nextMonth, previousMonth;
JButton saveDailyRecord, deleteDailyRecord, readDailyRecord;
File dir;
Color backColor = Color.white;
public CalendarWindow() {
dir = new File("./dailyRecord");
dir.mkdir();
showDay = new JTextField[42];
for (int i = 0; i < showDay.length; i++) {
showDay[i] = new JTextField();
showDay[i].setBackground(backColor);
showDay[i].setLayout(new GridLayout(3, 3));
showDay[i].addMouseListener(this);
showDay[i].addFocusListener(this);
}
calendarMessage = new CalendarMessage();
calendarPad = new CalendarPad();
notePad = new NotePad();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
calendarMessage.setYear(year);
calendarMessage.setMonth(month);
calendarMessage.setDay(day);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.setShowDayTextField(showDay);
notePad.setMessage(year, month, day);
calendarPad.showMonthCalendar();
doMark();
calendarImage = new CalendarImage();
picturename = getPicture_address();//
calendarImage.setImageFile(new File(picturename));
JMenuBar menuBar = new JMenuBar();
JMenu menusetting = new JMenu();
JMenuItem changepicture = new JMenuItem();
menusetting.setText("图像");//设置标题
changepicture.setText("更改图片背景");//设置标题
//添加菜单条上去;
menusetting.add(changepicture);
menuBar.add(menusetting);
setJMenuBar(menuBar);
//注册监听器
changepicture.addActionListener(this);
clock = new Clock();
JSplitPane splitV1 = new
JSplitPane(JSplitPane.VERTICAL_SPLIT, calendarPad, calendarImage);
JSplitPane splitV2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, notePad, clock);
JSplitPane splitH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, splitV1, splitV2);
add(splitH, BorderLayout.CENTER);
showYear = new JTextField("" + year, 6);
showYear.setFont(new Font("TimesRoman", Font.BOLD, 12));
showYear.setHorizontalAlignment(JTextField.CENTER);
showMonth = new JTextField("" + month, 4);
showMonth.setFont(new Font("TimesRoman", Font.BOLD, 12));
showMonth.setHorizontalAlignment(JTextField.CENTER);
nextYear = new JButton("下年");
previousYear = new JButton("上年");
nextMonth = new JButton("下月");
previousMonth = new JButton("上月");
nextYear.addActionListener(this);
previousYear.addActionListener(this);
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
showYear.addActionListener(this);
JPanel north = new JPanel();
north.add(previousYear);
north.add(showYear);
north.add(nextYear);
north.add(previousMonth);
north.add(showMonth);
north.add(nextMonth);
add(north, BorderLayout.NORTH);
saveDailyRecord = new JButton("保存日志");
deleteDailyRecord = new JButton("删除日志");
readDailyRecord = new JButton("读取日志");
saveDailyRecord.addActionListener(this);
deleteDailyRecord.addActionListener(this);
readDailyRecord.addActionListener(this);
JPanel pSouth = new JPanel();
pSouth.add(saveDailyRecord);
pSouth.add(deleteDailyRecord);
pSouth.add(readDailyRecord);
add(pSouth, BorderLayout.SOUTH);
setVisible(true);
setBounds(70, 70, 770, 500);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public String getPicture_address() {
String address = null;
try {
FileInputStream outOne = new FileInputStream("picture_address.txt");
ObjectInputStream outTwo = new ObjectInputStream(outOne);
try {
address = (String) outTwo.readObject();
} catch (Exception ex) {
}
outTwo.close();
} catch (IOException eee) {
}
if (address != null) {
return address;
} else {
return "picture.jpg";
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("更改图片背景")) {
FileDialog dia = new FileDialog(this, "选定图片", FileDialog.LOAD);//新建一个文件选择对
话框,用 dia 获得选择得到的信息;
dia.setModal(true);//设置为模态对话框;
dia.setVisible(true);
//将取得的内容存入“picture_address.txt”文件中, 下次打开软件时还能记住上次的图片路径的选择;
if ((dia.getDirectory() != null) && (dia.getFile() != null))
//如果得到的路径和文件名不为空;
{
try {
FileOutputStream inOne = new FileOutputStream("picture_address.txt");
ObjectOutputStream inTwo = new ObjectOutputStream(inOne);
inTwo.writeObject(dia.getDirectory() + dia.getFile());
inTwo.close();
} catch (IOException ee) {
}
picturename = getPicture_address();
//从文件中读出显示图片的路径地址;
calendarImage.setImageFile(new File(picturename));
//将获得路径传给图片, 显示该路径指定的图片
}
}
if (e.getSource() == nextYear) {
year++;
showYear.setText("" + year);
calendarMessage.setYear(year);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.showMonthCalendar();
notePad.setMessage(year, month, day);
doMark();
} else if (e.getSource() == previousYear) {
year--;
showYear.setText("" + year);
calendarMessage.setYear(year);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.showMonthCalendar();
notePad.setMessage(year, month, day);
doMark();
} else if (e.getSource() == nextMonth) {
month++;
if (month > 12) month = 1;
showMonth.setText("" + month);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.showMonthCalendar();
notePad.setMessage(year, month, day);
doMark();
} else if (e.getSource() == previousMonth) {
month--;
if (month < 1) month = 12;
showMonth.setText("" + month);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.showMonthCalendar();
notePad.setMessage(year, month, day);
doMark();
} else if (e.getSource() == showYear) {
String s = showYear.getText().trim();
char a[] = s.toCharArray();
boolean boo = false;
for (int i = 0; i < a.length; i++) {
if (!(Character.isDigit(a[i])))
boo = true;
}
if (boo == true)