package calendar;
import Java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class CalendarV2 extends Applet {
private boolean isStandalone = false;
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//ConstrUCt the applet
public CalendarV2() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
jLabel1.setText("Please Enter Year");
jLabel1.setBounds(new Rectangle(8, 30, 114, 26));
this.setLayout(null);
jTextField1.setText("");
jTextField1.setBounds(new Rectangle(129, 30, 136, 27));
jLabel2.setText("Please Ente Month");
jLabel2.setBounds(new Rectangle(4, 70, 104, 26));
jTextField2.setText("");
jTextField2.setBounds(new Rectangle(130, 69, 136, 26));
jButton1.setBounds(new Rectangle(16, 122, 233, 21));
jButton1.setText("CheckCalendarV2");
jButton1.addActionListener(new CalendarV2_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(16, 156, 232, 20));
jButton2.setText("Exit");
jButton2.addActionListener(new CalendarV2_jButton2_actionAdapter(this));
this.add(jButton1, null);
this.add(jTextField1, null);
this.add(jTextField2, null);
this.add(jLabel2, null);
this.add(jLabel1, null);
this.add(jButton2, null);
}
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;
}
//--------------------------------
//------查看是否闰年----------------
public boolean checkLeapYear(int Year){
if(Year%4==0&&Year%100!=0){
isLeapYear=true;
}
if(Year%400==0)
isLeapYear=true;
else if(Year%4!=0){
isLeapYear=false;
}
return isLeapYear;
}
//---------------------------------
//--------计算当月天数---------------
//要输入年份的原因是要判定二月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****/
int AllDates=0;
String a,b;
a=jTextField1.getText();
b=jTextField2.getText();
EnterYear=Integer.parseInt(a);
EnterMonth=Integer.parseInt(b);
if(EnterYear>=2001){
AllDates=this.counterYearDates(EnterYear)
+this.counterMonthDates(EnterMonth,EnterYear)+1;
}
else if(EnterYear>0){
AllDates=-this.counterYearDates(EnterYear)
+this.counterMonthDates(EnterMonth,EnterYear);
}
else
System.out.print("Year Error");
printTable(Array(AllDates,EnterMonth));
isEverPressBtn=true;
}
JButton jButton2 = new JButton();
void jButton2_actionPerformed(ActionEvent e) {
System.exit(1);
}
//--------------------------------------------------------
//***********************************************************************************
//Methods
//***********************************************************************************
}
class CalendarV2_jButton1_actionAdapter implements java.awt.event.ActionListener {
CalendarV2 adaptee;
CalendarV2_jButton1_actionAdapter(CalendarV2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class CalendarV2_jButton2_actionAdapter implements java.awt.event.ActionListener {
CalendarV2 adaptee;
CalendarV2_jButton2_actionAdapter(CalendarV2 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}