import java.awt.*;
import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.JButton;
public class Wan extends Frame implements ActionListener, ItemListener,
Runnable {
Thread time;
int Y=2008,M=10;
Font font=new Font("楷体-GB2312",Font.BOLD,12);
Color color=new Color(231,232,255);
Choice year,month;
Panel p1,p2,p3;
GridLayout layout=new GridLayout(7,7,10,10);
Button today,exit;
String 月[]={"01","02","03","04","05","06","07","08","09","10","11","12"};
Label la1,la2,la3,la4;
Label []label=new Label[49];
String weekDay[]= {" 日", " 一"," 二"," 三"," 四"," 五"," 六"};
Wan()
{
super("万年历");
this.setBackground(color);
this.setResizable(false);
p1=new Panel();
la1=new Label("在此处查询");
la1.setForeground(Color.blue);
la1.setFont(font);
p1.add(la1);
year=new Choice();
p1.add(year);
for(int i=1800;i<=3000;i++)
year.add(""+i);
year.select("2008");
year.setFont(font);
year.setForeground(Color.blue);
year.addItemListener(this);
la2=new Label("年");
la2.setFont(font);
la2.setForeground(Color.blue);
p1.add(la2);
month=new Choice();
for(int i=0;i<12;i++)
month.add(月[i]);
month.select("10");
month.setFont(font);
month.setForeground(Color.blue);
month.addItemListener(this);
p1.add(month);
la3=new Label("月");
la3.setFont(font);
la3.setForeground(Color.blue);
p1.add(la3);
today=new Button("今日");
today.addActionListener(this);
p1.add(today);
this.add(p1, "North");
p2=new Panel();
p2.setLayout(layout);
for(int i=0;i<7;i++)
{
label[i]=new Label(weekDay[i]);
label[i].setFont(font);
label[i].setForeground(Color.blue);
p2.add(label[i]);
}
for(int i=7;i<49;i++)
{
label[i]=new Label();
label[i].setFont(font);
label[i].setForeground(Color.blue);
p2.add(label[i]);
}
this.add(p2, "Center");
update(2008,10);
p3=new Panel();
la4=new Label();
la4.setFont(font);
la4.setForeground(Color.blue);
p3.add(la4);
exit=new Button("退出");
exit.addActionListener(this);
p3.add(exit);
time=new Thread(this);
time.setDaemon(true);
time.start();
this.add(p3,"South");
layout=new GridLayout(7,7,10,10);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
this.setVisible(true);
this.setBounds(60, 60, 450, 400);
this.validate();
}
public void update(int y,int m)
{
int n=0,s,i,j,k,r;
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
j=calendar.get(Calendar.YEAR);
k=calendar.get(Calendar.MONTH)+1;
r=calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(y, m-1, 1);
s=calendar.get(Calendar.DAY_OF_WEEK);
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
n=31;
break;
case 4:
case 6:
case 9:
case 11:
n=30;
break;
case 2:
{
if((y%100!=0&&y%4==0)||y%400==0)
n=29;
else
n=28;
break;
}
default: n=0;break;
}
for(i=7;i<s+6;i++)
label[i].setText(null);
for(int t=1;t<=n;i++,t++)
{
label[i].setText(" "+t);
if(i%7==0||i%7==6)
{
label[i].setForeground(Color.red);
label[i].setBackground(color);
}
if(Y==j&&M==k)
label[r+s+5].setBackground(Color.green);
}
for(;i<49;i++)
{
label[i].setText(null);
label[i].setBackground(color);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==exit)
this.dispose();
else if(e.getSource()==today)
{
Y=2008;
M=10;
update(Y,M);
}
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==year)
{
Y=Integer.parseInt(year.getSelectedItem());
update(Y,M);
}
else if(e.getSource()==month)
{
M=Integer.parseInt(month.getSelectedItem());
update(Y,M);
}
}
public void run() {
// TODO Auto-generated method stub
Date date;
String str;
SimpleDateFormat format=new SimpleDateFormat("北京时间:yyyy年MM月dd日HH时mm分ss秒");
while(true)
{
try
{
date=new Date();
str=format.format(date);
la4.setText(str);
Thread.sleep(1000);
}
catch(InterruptedException ee){}
}
}
}