importjava.util.Calendar;publicclassCalendarBean{Stringday[];intyear=2005,month=0;publicvoidsetYear(intyear){this.year=year;}publicintgetYear(){returnyear;}publicvoidsetMonth(intmonth){this.month=month;}publicintgetMonth(){returnmonth;}publicString[]getCalendar(){Stringa[]=newString[42];Calendardate=Calendar.getInstance();date.set(year,month-1,1);intweek=date.get(Calendar.DAY_OF_WEEK)-1;intday=0;//ÅжϴóÔ·Ýif(month==1||month==3||month==5||month==7||month==8||month==10||month==12){day=31;}//ÅжÏСÔÂif(month==4||month==6||month==9||month==11){day=30;}//ÅжÏƽÄêÓëÈòÄêif(month==2){if(((year%4==0)&&(year%100!=0))||(year%400==0)){day=29;}else{day=28;}}for(inti=week,n=1;iweek+day;i++){a[i]=String.valueOf(n);n++;}returna;}}CalendarFrame.javaimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassCalendarFrameextendsJFrameimplementsActionListener{JLabellabelDay[]=newJLabel[42];JTextFieldtext=newJTextField(10);JButtontitleName[]=newJButton[7];JButtonbutton=newJButton();Stringname[]={ÈÕ,Ò»,¶þ,Èý,ËÄ,Îå,Áù};JButtonnextMonth,previousMonth;intyear=1996,month=1;//Æô¶¯³ÌÐòÏÔʾµÄÈÕÆÚÐÅÏ¢CalendarBeancalendar;JLabelshowMessage=newJLabel(,JLabel.CENTER);JLabellbl1=newJLabel(ÇëÊäÈëÄê·Ý£º);JLabellbl2=newJLabel();publicCalendarFrame(){JPanelpCenter=newJPanel();//½«pCenterµÄ²¼¾ÖÉèÖÃΪ7ÐÐ7ÁеÄGridLayout²¼¾Ö¡£pCenter.setLayout(newGridLayout(7,7));//pCenterÌí¼Ó×é¼þtitleName[i]for(inti=0;i7;i++){titleName[i]=newJButton(name[i]);pCenter.add(titleName[i]);}//pCenterÌí¼Ó×é¼þlabelDay[i]for(inti=0;i42;i++){labelDay[i]=newJLabel(,JLabel.CENTER);pCenter.add(labelDay[i]);}text.addActionListener(this);calendar=newCalendarBean();calendar.setYear(year);calendar.setMonth(month);Stringday[]=calendar.getCalendar();for(inti=0;i42;i++){labelDay[i].setText(day[i]);}nextMonth=newJButton(ÏÂÔÂ);previousMonth=newJButton(ÉÏÔÂ);button=newJButton(È·¶¨);//×¢²á¼àÌýÆ÷nextMonth.addActionListener(this);previousMonth.addActionListener(this);button.addActionListener(this);JPanelpNorth=newJPanel(),pSouth=newJPanel();pNorth.add(showMessage);pNorth.add(lbl2);pNorth.add(previousMonth);pNorth.add(nextMonth);pSouth.add(lbl1);pSouth.add(text);pSouth.add(button);showMessage.setText(ÈÕÀú£º+calendar.getYear()+Äê+calendar.getMonth()+ÔÂ);ScrollPanescrollPane=newScrollPane();scrollPane.add(pCenter);add(scrollPane,BorderLayout.CENTER);//´°¿ÚÌí¼ÓscrollPaneÔÚÖÐÐÄÇøÓòadd(pNorth,BorderLayout.NORTH);//´°¿ÚÌí¼ÓpNorthÔÚ±±ÃæÇøÓòadd(pSouth,BorderLayout.SOUTH);//´°¿ÚÌí¼ÓpSouthÔÚÄÏÇøÓò¡£}publicvoidactionPerformed(ActionEvente){if(e.getSource()==nextMonth){month=month+1;if(month12)month=1;calendar.setMonth(month);Stringday[]=calendar.getCalendar();for(inti=0;i42;i++){labelDay[i].setText(day[i]);}}elseif(e.getSource()==previousMonth){month=month-1;if(month1)month=12;calendar.setMonth(month);Stringday[]=calendar.getCalendar();for(inti=0;i42;i++){labelDay[i].setText(day[i]);}}elseif(e.getSource()==button){month=month+1;if(month12)month=1;calendar.setYear(Integer.parseInt(text.getText()));Stringday[]=calendar.getCalendar();for(inti=0;i42;i++){labelDay[i].setText(day[i]);}}showMessage.setText(ÈÕÀú£º+calendar.getYear()+Äê+calendar.getMonth()+ÔÂ);}}CalendarMainClass.javaimportjavax.swing.JFrame;importjavax.swing.UIManager;publicclassCalendarMainClass{publicstaticvoidmain(Stringargs[]){try{UIManager.setLookAndFeel(com.sun.java.swing.plaf.windows.WindowsLookAndFeel);//windows½çÃæ·ç¸ñ}catch(Exceptione){e.printStackTrace();}CalendarFrameframe=newCalendarFrame();frame.setBounds(100,100,360,300);frame.setTitle(ÈÕÀúС³ÌÐò);frame.setLocationRelativeTo(null);//´°Ìå¾ÓÖÐÏÔʾframe.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}