?? calendercreator.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類進(jìn)行實例化名字為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()); ////////////////////////////////////調(diào)用FlowLayout方法
p2=new Panel(new GridLayout(7,7,9,8)); /////////////////////////////調(diào)用GridLayout方法,7行7個button , 左間隔9,上下間隔8
p1.setBackground(Color.white); /////////////////////////////////////背景色為白色
p2.setBackground(Color.white); /////////////////////////////////////背景色為白色
add(p1,BorderLayout.NORTH); ////////////////////////////////////////把p1放在邊框布局的北方,即上方
add(p2);
p1.add(ltext); /////////////////////////////////////////////////////調(diào)用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)); ////////////////////設(shè)置一個監(jiān)聽器
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++) /////////////////////////////////當(dāng)輸入查詢的年份的時候從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); ///////////////////////////////////////////////////////設(shè)置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月的天數(shù)
{
totdays=29;
setButtons(iday,totdays);
}
if( !gc.isLeapYear(iselYear) && iselMonth==1) ///////////////////////////判斷是否閏年從而決定2月的天數(shù)
{
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轉(zhuǎn)換成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); ///////////////////////////////////////////移除監(jiān)聽
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -