1: First create a Calendarbean class:
Code:
- Import Java.util.Calendar;
- Public class Calendarbean
- {
- String day[];
- int year=2005,month=0;
- Public void setyear (int year)
- {
- This.year=year;
- }
- public int GetYear ()
- {
- Return year;
- }
- Public void setmonth (int month)
- {
- This.month=month;
- }
- public int GetMonth ()
- {
- Return month;
- }
- Public string[] Getcalendar ()
- {
- String a[]=New string[42];
- Calendar date=calendar.getinstance ();
- Date.set (year,month-1,1);
- int Week=date.get (Calendar.day_of_week)-1;
- int day=0;
- Judging the big month
- if (month==1| | month==3| | month==5| | month==7
- || month==8| |month==10| | month==)
- {
- day=31;
- }
- Judging Xiao Yue
- if (month==4| | month==6| | month==9| | month==)
- {
- day=30;
- }
- Judging common year and leap years
- if (month==2)
- {
- if (((year%4==0) && (year%100!=0)) | | (year%400==0))
- {
- day=29;
- }
- Else
- {
- day=28;
- }
- }
- for (int i=week,n=1;i<week+day;i++)
- {
- A[i]=string.valueof (n);
- n++;
- }
- return A;
- }
- }
2: Create a Calendarframe class
Import javax.swing.*;
public class Calendarframe extends JFrame implements ActionListener {
JLabel Labelday[]=new jlabel[42];
JTextField text=new JTextField (10);
JButton Titlename[]=new jbutton[7];
JButton button=new JButton ();
String name[]={"Day", "one", "two", "three", "four", "five", "six"};
JButton Nextmonth,previousmonth;
int year=1996,month=1;
Calendarbean Calendar;
JLabel showmessage=new JLabel ("", jlabel.center);
JLabel lbl1=new JLabel ("Please enter the Year:");
JLabel lbl2=new JLabel ("");
Public Calendarframe () {
JPanel pcenter=new JPanel ();
Set the layout of the Pcenter to a gridlayout layout of 7 rows and 7 columns
Pcenter.setlayout (New GridLayout (7,7));
Pcenter adding components Titlename[i]
for (int i=0;i<7;i++) {
Titlename[i]=new JButton (Name[i]);
Pcenter.add (Titlename[i]);
}
Pcenter adding components Labelday[i]
for (int i=0;i<42;i++) {
Labelday[i]=new JLabel ("", jlabel.center);
Pcenter.add (Labelday[i]);
}
Text.addactionlistener (this);
Calendar=new Calendarbean ();
Calendar.setyear (year);
Calendar.setmonth (month);
String Day[]=calendar.getcalendar ();
for (int i=0;i<42;i++) {
Labelday[i].settext (Day[i]);
}
Nextmonth=new JButton ("next month");
Previousmonth=new JButton ("last month");
Button=new JButton ("OK");
Register Listener
Nextmonth.addactionlistener (this);
Previousmonth.addactionlistener (this);
Button.addactionlistener (this);
JPanel pnorth=new JPanel (),
Psouth=new JPanel ();
Pnorth.add (ShowMessage);
Pnorth.add (LBL2);
Pnorth.add (Previousmonth);
Pnorth.add (Nextmonth);
Psouth.add (LBL1);
Psouth.add (text);
Psouth.add (button);
Showmessage.settext ("Calendar" +calendar.getyear () + "Year" +calendar.getmonth () + "month");
ScrollPane scrollpane=new ScrollPane ();
Scrollpane.add (Pcenter);
Add (scrollpane,borderlayout.center);//window added to ScrollPane middle position
Add (Pnorth,borderlayout.north); Window add Pnorth to the north of the window
Add (Psouth,borderlayout.south); Window add Psouth to the south of the window
}
public void actionperformed (ActionEvent e) {
if (E.getsource () ==nextmonth) {
Month+=1;
if (month>12)
month=1;
Calendar.setmonth (month);
String Day[]=calendar.getcalendar ();
for (int i=0;i<42;i++) {
Labelday[i].settext (Day[i]);
}
}
else if (E.getsource () ==button) {
Month+=1;
if (month>12)
month=1;
Calendar.setyear (Integer.parseint (Text.gettext ()));
String Day[]=calendar.getcalendar ();
for (int i=0;i<42;i++) {
Labelday[i].settext (Day[i]);
}
Showmessage.settext ("Calendar" +calendar.getyear () + "Year" +calendar.getmonth () + "month");
}
}
}
3: Creating a Calendarmainclass class
Code:
Import Javax.swing.JFrame;
Import Javax.swing.UIManager;
public class Calendarmainclass {
public static void Main (String[]args) {
try{
Uimanager.setlookandfeel ("Com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception e) {
E.printstacktrace ();
}
Calendarframe frame=new calendarframe ();
Frame.setbounds (100,100,360,300);
Frame.settitle ("Chinese calendar");
Frame.setlocationrelativeto (null);//Form Center display
Frame.setvisible (TRUE);
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
}
}
Operation Result:
JAVA----Calendar Source code