package a;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Calendar;
import java.util.Hashtable;
public class Rili extends JFrame implements ActionListener, MouseListener {
int year, month, day;
Label label;//定义一个label标签
NotePad notepad;//定义一个类的对象
File file;//文件和目录路径名的抽象表示形式
JTextField showDay[];//定义一个单行文本数组
JLabel title[]; // 星期字段组标签
JButton button1, button2, button3, button4;
JTextField showYear = null;//定义一个文本框
JTextField showMonth = null;
// 定义字符串类型(星期)数组
String 星期[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar calendar; // 日历
int week;// 星期几
JPanel leftPanel, rightPanel;//定义两容器
//构造方法
public Rili(int year, int month, int day) {
super("日历");
this.setSize(600, 300);
// 左容器
//实例化对象
leftPanel = new JPanel();
JPanel leftCenter = new JPanel();
JPanel leftNorth = new JPanel();
// 使用网格布局方式
GridLayout myLayout = new GridLayout(7, 7);
leftCenter.setLayout(myLayout);// 把网格布局放入左容器中
button1 = new JButton("上年");
button2 = new JButton("下年");
button3 = new JButton("上月");
button4 = new JButton("下月");
showYear = new JTextField(4);
// 添加事件
showYear.addActionListener(this);
showYear.setText("" + year);// 向文本内添入数据
// showYear.setEditable(true);// 可修改文本年
showYear.setForeground(Color.black);
showMonth = new JTextField(2);
// 添加事件
showMonth.addActionListener(this);
showMonth.setText("" + month);
// showMonth.setEditable(true);// 可修改文本月
showMonth.setForeground(Color.black);
// 每个按钮都要添加监听事件
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
title = new JLabel[7];
showDay = new JTextField[42];//生成实例
//把星期字段添加到标签
for (int i = 0; i < 7; i++) {
title[i] = new JLabel();
title[i].setText(星期[i]);
title[i].setBorder(BorderFactory.createRaisedBevelBorder());//标签外表类型为凸
//添加到容器
leftCenter.add(title[i]);
}
title[0].setForeground(Color.red);
title[6].setForeground(Color.red);
for (int j = 0; j < 42; j++) {
showDay[j] = new JTextField();
leftCenter.add(showDay[j]);
}
for (int i = 0; i < 42; i++) {
showDay[i].addMouseListener(this);
}
calendar = Calendar.getInstance();
// 创建一个从左到右显示其组件的 Box
Box box = new Box(BoxLayout.X_AXIS);
//添加box容器
box.add(button1);
box.add(showYear);
box.add(button2);
box.add(button3);
box.add(showMonth);
box.add(button4);
leftNorth.add(box);
leftPanel.setLayout(new BorderLayout());
label = new Label("请在年份输入框输入所查年份(负数表示公元前),并回车确定");
leftPanel.add(label, BorderLayout.SOUTH);
leftPanel.add(leftCenter, BorderLayout.CENTER);
leftPanel.add(leftNorth, BorderLayout.NORTH);
leftPanel.validate();
rightPanel = new JPanel();
this.year = year;
this.month = month;
this.day = day;
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
leftPanel, rightPanel);
split.setDividerLocation(350);// 设置JSplitPane对象splitPane1的分隔线位置
this.add(split);
rili(year, month);
this.setVisible(true);
this.setResizable(false);
// 文件
//从文件中读内容
file = new File("日历记事本.txt");
Hashtable hashtable = new Hashtable();
if (!file.exists()) {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(hashtable);
out.close();
} catch (IOException e) {
}
}
// 右容器
notepad = new NotePad(this);
rightPanel.add(notepad);
notepad.top(year, month, day);
}
// 年
public void setshowYear(int year) {
this.year = year;
showYear.setText("" + year);
}
public int getshowYear() {
return year;
}
//月
public void setshowMonth(int month) {
if (month <= 12 && month >= 1) {
this.month = month;
} else {
this.month = 1;
}
showMonth.setText("" + month);
}
public int getshowMonth() {
return month;
}
// 设置上年、下年、上月、下月四个按钮
public void actionPerformed(ActionEvent e) {
//判断上年、下年
if (e.getSource() == button1) {
year = year - 1;
showYear.setText("" + year);
setshowYear(year);
rili(year, getshowMonth());
notepad.top(year, month, day);
}
if (e.getSource() == button2) {
year = year + 1;
showYear.setText("" + year);
setshowYear(year);
rili(year, getshowMonth());
notepad.top(year, month, day);
}
//判断上月、下月
if (e.getSource() == button3) {
if (month >= 2) {
month = month - 1;
setshowMonth(month);
rili(getshowYear(), month);
notepad.top(year, month, day);
} else if (month == 1) {
month = 12;
setshowMonth(month);
rili(getshowYear(), month);
notepad.top(year, month, day);
}
showMonth.setText("" + month);
} else if (e.getSource() == button4) {
if (month < 12) {
month = month + 1;
setshowMonth(month);
rili(getshowYear(), month);
} else if (month == 12) {
month = 1;
setshowMonth(month);
rili(getshowYear(), month);
}
showMonth.setText("" + month);
}
if (e.getSource() == showYear) {
try {
year = Integer.valueOf(showYear.getText());
} catch (Exception ee) {
} finally {
rili(year, month);
showYear.setText(String.valueOf(year));
}
}
if (e.getSource() == showMonth) {
try {
month = Integer.valueOf(showMonth.getText());
} catch (Exception a) {
} finally {
rili(year, month);
showMonth.setText(String.valueOf(month));
}
}
}
// 编排号码
public void number(int week, int mDay) {
for (int i = week, n = 1; i < week + mDay; i++) {
showDay[i].setText("" + n);
showDay[i].setEditable(false);
if (n == day) {
showDay[i].setForeground(Color.red);
showDay[i].setFont(new Font("TimesRoman", Font.BOLD, 22));
} else {
showDay[i].setForeground(Color.black);
showDay[i].setFont(new Font("TimesRoman", Font.BOLD, 12));
}
if (i % 7 == 0) {
showDay[i].setBackground(Color.white);
}
if (i % 7 == 6) {
showDay[i].setBackground(Color.white);
}
n++;
}
for (int i = 0; i < week; i++) {
勇敢爱
- 粉丝: 34
- 资源: 151
- 1
- 2
- 3
- 4
前往页