import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyAdapter implements ActionListener
{
FrameCalendar fc;
public MyAdapter(FrameCalendar fc1)
{
fc=fc1;
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==fc.btnDisplay)
{
int month,year;
GregorianCalendar c = new GregorianCalendar();
try
{
month=Integer.parseInt(fc.tMonth.getText());
year=Integer.parseInt(fc.tYear.getText());
} catch (NumberFormatException e)
{ month=c.get(Calendar.MONTH)+1;
year=c.get(Calendar.YEAR);
}
c.set(Calendar.DATE,1);
c.set(Calendar.MONTH,month-1);
c.set(Calendar.YEAR,year);
printCalendar(c);
}
}
private void printCalendar(GregorianCalendar c)
{
String DAY_OF_WEEK[] = {"Su","Mo","Tu","We","Th","Fr","Sa"};
int monthLength[]={31,28,31,30,31,30,31,31,30,31,30,31};
int MonthArray[][]=new int[6][7];
int month,year,date;
for (int i=0;i<6;i++)
{
Arrays.fill(MonthArray[i],0);
}
c.set(Calendar.DATE,1);
date=c.get(Calendar.DATE);
month=c.get(Calendar.MONTH);
year=c.get(Calendar.YEAR);
int i;
for (i=0;i
System.out.print(DAY_OF_WEEK[i]+"\t");
fc.ta.append(DAY_OF_WEEK[i]+"\t");
}
System.out.println();
if ((month==1) && (c.isLeapYear(year)))
monthLength[1]=29;
else
monthLength[1]=28;
for (i=1;i<=monthLength[month];i++)
{
MonthArray[c.get(Calendar.WEEK_OF_MONTH)-1][c.get(Calendar.DAY_OF_WEEK)-1]=i;
c.roll(Calendar.DATE,1);
}
for (i=0;i<6;i++)
{
System.out.println();
fc.ta.append("\n");
for (int j=0;j<7;j++)
{
if (MonthArray[i][j]==0)
{
System.out.print("\t");
fc.ta.append("\t");
}
else
{
System.out.print(MonthArray[i][j] + "\t");
fc.ta.append(MonthArray[i][j] + "\t");
}
}
}
}
}
public class FrameCalendar extends Frame
{
Frame f1;
Label l1;
Button btnDisplay;
Panel p1,p2;
TextField tMonth, tYear;
TextArea ta;
public FrameCalendar(){
f1=new Frame();
f1.setSize(400,400);
l1=new Label("Please enter Month and Year");
btnDisplay=new Button("Show");
tMonth=new TextField("Enter Month");
tYear=new TextField("Enter Year");
p1=new Panel();
p1.add(l1);
p1.add(btnDisplay);
p1.add(tMonth);
p1.add(tYear);
f1.add(p1);
p2=new Panel();
ta = new TextArea("",10,50,0);
p2.add(ta);
f1.add(p2);
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
f1.setLayout(new GridLayout(2,1));
f1.setVisible(true);
p1.setVisible(true);
p2.setVisible(true);
btnDisplay.addActionListener(new MyAdapter(this));
}
public static void main(String args[])
{
FrameCalendar fc = new FrameCalendar();
}
}
No comments:
Post a Comment