JLabel jLabel1 = new JLabel(); JTextField jTextField1 = new JTextField(); JLabel jLabel2 = new JLabel(); JTextField jTextField2 = new JTextField(); JButton jButton1 = new JButton();
//Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; } //Main method public static void main(String[] args) { CalendarV2 applet = new CalendarV2(); applet.isStandalone = true; Frame frame; frame = new Frame(); frame.setTitle("Applet Frame"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(400,320); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } // Declare dataMember //********************
boolean isLeapYear,isEverPressBTn=false; int thisYear,EnterYear,EnterMonth; //*********************************************************************************** //Methods //*********************************************************************************** //-----計算該年天數--------------- public int checkYear(int Year){ if(Year%4==0&&Year%100!=0){ thisYear = 366; } if(Year%100==0&&Year%400==0){ thisYear = 366; } else if(Year%4!=0){ thisYear=365; } return thisYear; } //--------------------------------
//--------計算當月天數--------------- //要輸入年份的原因是要判定二月29天還是28天 public int checkMonth(int Month ,int Year){ int Dates=0; if (Month <0Month>12){ System.out.println("Month Error"); } if(Month==1Month==3Month==5 Month==7Month==8Month==10 Month==12){ Dates=31; } if(Month==2&&this.checkLeapYear(Year)){ Dates=29; } if(Month==2&&!this.checkLeapYear(Year)){ Dates=28; } if(Month==4Month==6Month==9Month==11){ Dates=30; } return Dates; } //------------------------------------
//-----用遞歸法計算目標年到已知年的總天數--- public int counterYearDates(int EnterYear){ int sum1=0; if(EnterYear==2001){//2001年為已知年 sum1=0; //遞歸從此跳出 } if(EnterYear>2001){ sum1+=this.checkYear(EnterYear-1) +this.counterYearDates(EnterYear-1); } //當目標年大于2001年,則從目標年-1往前累加 //到2001年為止 if(EnterYear<2001){ sum1+=this.checkYear(EnterYear) +this.counterYearDates(EnterYear+1); } //當目標年小于2001年,則從目標年往后累加 //到2001年為止 return sum1; } //-----計算目標年從一月到目標月的總天數------------------------ //要輸入年份的原因是要判定二月29天還是28天 public int counterMonthDates(int EnterMonth,int EnterYear){ int sum2=0; if(EnterMonth==1){ sum2=0; } if(EnterMonth>1&&EnterMonth<=12){ sum2+=this.checkMonth(EnterMonth-1,EnterYear) +this.counterMonthDates(EnterMonth-1,EnterYear); } else if(EnterMonth<0){ System.out.print("Month Error"); } return sum2; } //------------------------------------------------
//-------------用數組存目標月日期----------------------------- public int[] Array(int AllDates,int EnterMonth){ int n=AllDates%7; int a=1; int[] DayInTable=new int[38]; if(n<0)//Keep the n bigger than 0 n=7+n+1;//Line 207 haven′t add 1,so here add if(n!=0) for(int i=n;i<this.checkMonth(EnterMonth,EnterYear)+n;i++){ DayInTable[i]=a; a++; } else for(int i=7;i<this.checkMonth(EnterMonth,EnterYear)+7;i++){ //若n為0,則說明目標月一號為星期日 DayInTable[i]=a; a++; } return DayInTable; } //----------------------------------------------------------
//-----------打印輸出------------------------------- public void printTable(int[]DayInTable){ System.out.println("Curren Date is: Year " +EnterYear+" Month "+EnterMonth); System.out.print("Mon Tue Wed Thur Fri Sat Sun "); System.out.println(); for(int i=1;i<=37;i++){ if(DayInTable[i]==0) System.out.print(" "); if(i%7==0&&DayInTable[i]!=0){ //防止在最后i=35時輸出0 System.out.print(DayInTable[i]+" "); System.out.println(); } else if(DayInTable[i]>9) System.out.print(DayInTable[i]+" "); else if(DayInTable[i]!=0) //防止在最后i<9時輸出0 System.out.print(DayInTable[i]+" "); if(i==37) System.out.println(); } } //-----------------------------------------------------
//---------------按鈕觸發事件--------------------- void jButton1_actionPerformed(ActionEvent e) { /* ***********refresh Datas****************** if(isEverPressBtn){ Dates=0;AllDates=0;sum1=0;sum2=0;thisYear=0; /*假如這幾個變量在方法里聲明,則不用刷新,請看CalendarV2*/ //} /******Left All Datas go back to 0****/