import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
public class MyCalendar extends Frame implements ItemListener{
private static final long serialVersionUID = 1L;
Panel panel = new Panel(new BorderLayout());
Panel panel1 = new Panel();
Panel panel2 = new Panel(new GridLayout(7, 7)) ;//日历的布局
Label[] label = new Label[49];
Label year_label = new Label("年份");
Label month_label = new Label("月份");
Choice Choice_year = new Choice();//下拉菜单
Choice Choice_month = new Choice();
int x, y;//坐标
Calendar now = Calendar.getInstance(); // 实例化Calendar
MyCalendar() {
super("万年历");
setSize(300, 350);
x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x - 300) / 2, (y - 350) / 2);
//setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
panel1.add(year_label);
panel1.add(Choice_year);
panel1.add(month_label);
panel1.add(Choice_month);
for (int i = 0; i < 49; i++) {
label[i] = new Label("", Label.CENTER);// 将显示的字符设置为居中
panel2.add(label[i]);
}
panel.add(panel1, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.CENTER);
panel.setBackground(Color.white);
panel1.setBackground(Color.white);
panel2.setBackground(Color.white);
Init();//初始化
Choice_year.addItemListener(this);
Choice_month.addItemListener(this);
addWindowListener(new WindowAdapter()//关闭窗口
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
add(panel);
setVisible(true);
setResizable(false);
validate();
}
public void itemStateChanged(ItemEvent e) {
int year, month, week;
year = Integer.parseInt( Choice_year.getSelectedItem().toString()); // 得到当前所选年份
month = Integer.parseInt( Choice_month.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
week = use(year, month); // 调用函数use,得到星期几
Resetday(week, year, month); // 调用函数Resetday
}
public void Init() {
int year, month_num, first_day_num;
String log[] = { "日", "一", "二", "三", "四", "五", "六" };
for (int i = 0; i < 7; i++) {
label[i].setText(log[i]);
}
for (int i = 0; i < 49; i = i + 7) {
label[i].setForeground(Color.red); // 将星期日的日期设置为红色
}
for (int i = 6; i < 49; i = i + 7) {
label[i].setForeground(Color.green);// 将星期六的日期设置为绿色
}
for (int i = 1; i < 10000; i++) {//万年
Choice_year.addItem("" + i);
}
for (int i = 1; i < 13; i++) {
Choice_month.addItem("" + i);
}
month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份
year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份
Choice_year.select(year - 1); // 设置下拉列表显示为当前年
Choice_month.select(month_num); // 设置下拉列表显示为当前月
first_day_num = use(year, month_num);
Resetday(first_day_num, year, month_num);
}
public int use(int reyear, int remonth) {
int week_num;
now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
return week_num;
}
public void Resetday(int week_log, int year_log, int month_log) {
int month_day_score; // 存储月份的天数
int count;
month_day_score = 0;
count = 1;
Calendar cal = Calendar.getInstance();
cal.set(year_log, month_log + 1, 1);
cal.add(Calendar.MONTH, -1); // 前个月
month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天
for (int i = 7; i < 49; i++) { // 初始化标签
label[i].setText("");
}
week_log = week_log + 6; // 将星期数加6,使显示正确
month_day_score = month_day_score + week_log;
for (int i = week_log; i < month_day_score; i++, count++) {
label[i].setText(count + "");
}
}
public static void main(String[] args) {//主函数
new MyCalendar();
}
}