java编写的万年历

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class CalenderCreator extends Frame
{

Button days[]=new Button[49]; /////////////////////////////////////49的意思是一个7*7的button矩阵
Choice Month=new Choice();
Choice Year=new Choice();
Label lmonth=new Label("月份");
Label lyear=new Label("年份");
Label ltext=new Label("请选择查询的截止年份:");/////////////////输入要查询的年份
Panel p1,p2; ///////////////////////////定义面板p1,p2
GregorianCalendar gc=new GregorianCalendar(); /////////////////////对GregorianCalendar类进行实例化名字为gc
int totdays;
TextField textfield=new TextField(2);


public CalenderCreator()
{
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
System.exit(0);
}
});
setTitle("万年历 06专升本一班 06303103 冯占魁");
setSize(550,450); //////////////////////////////////////////////////定义万年历方框的大小为550*450
setResizable(false);
setLocation(20,20);
p1=new Panel(new FlowLayout()); ////////////////////////////////////调用FlowLayout方法
p2=new Panel(new GridLayout(7,7,9,8)); /////////////////////////////调用GridLayout方法,7行7个button , 左间隔9,上下间隔8
p1.setBackground(Color.white); /////////////////////////////////////背景色为白色
p2.setBackground(Color.white); /////////////////////////////////////背景色为白色
add(p1,BorderLayout.NORTH); ////////////////////////////////////////把p1放在边框布局的北方,即上方
add(p2);

p1.add(ltext); /////////////////////////////////////////////////////调用ltext
p1.add(textfield); /////////////////////////////////////////////////输入年份
p1.add(lmonth);
p1.add(Month);
Month.add("一月");
Month.add("二月");
Month.add("三月");
Month.add("四月");
Month.add("五月");
Month.add("六月");
Month.add("七月");
Month.add("八月");
Month.add("九月");
Month.add("十月");
Month.add("十一月");
Month.add("十二月");
Month.addItemListener(new myLis(this));
textfield.addActionListener(new myAction(this)); ////////////////////设置一个监听器


p1.add(lyear);
p1.add(Year);
Year.add("1900");

Year.addItemListener(new myLis(this));

for(int i=0;i<49;i++)
{
days[i]=new Button("");
}

for(int c=0;c<49;c++)
{
p2.add(days[c]);
}
setVisible(true); ////////////////////////////////////////////////////使这个组件可见
}
void setYear(String mynewyear)
{
int h=Integer.parseInt(mynewyear);
for(int adder=1901;adder<=h;adder++) /////////////////////////////////当输入查询的年份的时候从1900年开始
{
Year.add(""+adder);
}
}
void setButtons(int myday,int mytotdays)
{
int count=7;

days[0].setLabel("Sun");
days[1].setLabel("Mon");
days[2].setLabel("Tur");
days[3].setLabel("Wen");
days[4].setLabel("Tir");
days[5].setLabel("

Fir");
days[6].setLabel("Sat");

if ( myday>0)
{
int blank= myday;
for( ;blank>0;blank--,count++)
{
days[count].setLabel("");
}
}
for(int i=1;i<=mytotdays; i++,count++)
{
days[count].setLabel(""+i); ////////////////////////////////////////////日期加1
}
for(int j = 1;count < 49; j++,count++)
{
days[count].setLabel("");
}
}

void setVal(Date date,int iday,int iselMonth,int iselYear)
{
gc.setTime(date); ///////////////////////////////////////////////////////设置date 对象的日期和时间值
if(iselMonth==0 || iselMonth==2 || iselMonth==4 || iselMonth==6 || iselMonth== 7 ||iselMonth==9 || iselMonth==11)
/////////////////////////////每月31天的月
{
totdays=31;
setButtons(iday,totdays);
}

if(iselMonth==3 || iselMonth==5 || iselMonth==8 || iselMonth==10) ///////每月30天的月
{
totdays=30;
setButtons(iday,totdays);
}

if(gc.isLeapYear(iselYear) && iselMonth==1) /////////////////////////////判断是否闰年从而决定2月的天数
{
totdays=29;
setButtons(iday,totdays);
}
if( !gc.isLeapYear(iselYear) && iselMonth==1) ///////////////////////////判断是否闰年从而决定2月的天数
{
totdays=28;
setButtons(iday,totdays);
}

}


static public void main(String args[])
{
CalenderCreator c=new CalenderCreator(); ////////////////////////////////把CalenderCreator类实例化
}
}
class myLis implements ItemListener
{
CalenderCreator calLis; /////////////////////////////////////////////////calLis 是对CalenderCreator的引用
public myLis(CalenderCreator c)
{
calLis=c; ///////////////////////////////////////////////////////////////把c的值给calLis
}
public void itemStateChanged(ItemEvent i)
{
int selMonth=calLis.Month.getSelectedIndex(); ///////////////////////////做翻月用
int selYear1=Integer.parseInt(calLis.Year.getSelectedItem()); ///////////把整形对象Integer转换成int
int selYear = selYear1- 1900;
Date d1 = new Date(selYear,selMonth,1);
int day = d1.getDay();
calLis.setVal(d1,day,selMonth,selYear);
}
}
class myAction implements ActionListener
{
CalenderCreator calAc; //////////////////////////////////////////////////calAc 是对CalenderCreator的引用

int newyear;

public myAction(CalenderCreator ca)
{
calAc=ca;
}
public void actionPerformed(ActionEvent e)
{
String s=calAc.textfield.getText();
System.out.println("请选择查询的截止年份:"+s);
calAc.setYear(s);
TextField tf = (TextField)e.getSource(); /////////////////////////////////返回事件
tf.removeActionListener(this); ///////////////////////////////////////////移除监听
}
}

相关文档
最新文档