The following is the Java calendar program code in a book. Very practical.
Package example;
Import java. AWT. borderlayout;
Import java. AWT. color;
Import java. AWT. gridlayout;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;
Import java. util. date;
Import java. util. gregoriancalendar;
Import javax. Swing. jbutton;
Import javax. Swing. jcombobox;
Import javax. Swing. jframe;
Import javax. Swing. jlabel;
Import javax. Swing. jpanel;
Import java. AWT. Font ;;
/**
* Calendar Program
*/
Public class calendertrain extends jframe implements actionlistener {
/**
*
*/
Private Static final long serialversionuid = 1l;
// Drop-down list box for month and year
Private jcombobox monthbox = new jcombobox ();
Private jcombobox yearbox = new jcombobox ();
// Month and year labels
Private jlabel yearlabel = new jlabel ("year :");
Private jlabel monthlabel = new jlabel ("month :");
// "View" and "today" buttons
Private jbutton button_ OK = new jbutton ("check ");
Private jbutton button_today = new jbutton ("today ");
// Obtain the date, year, and month of the current day.
Private date now_date = new date ();
Private int now_year = fig () + 1900;
Private int now_month = now_date.getmonth ();
Private Boolean todayflag = false; // whether to display today's date
// Display the date with a group of buttons. There are 7 rows and 7 columns in total, with the name of the first behavior week
Private jbutton [] button_day = new jbutton [42];
Private final string [] week = {"day", "one", "two", "three", "four", "five", "Six "};
Private jbutton [] button_week = new jbutton [7];
Private string year_int = NULL; // Save the year selected
Private int month_int; // Save the month selected
/**
* Constructor
*/
Public calendertrain (){
Super ();
System. Out. Print (now_month );
This. settitle ("calendar"); // you can specify the title.
This. INIT ();
This. setlocation (500,300 );
This. setresizable (true); // The Panel size cannot be changed.
Pack ();
}
/**
*
* Initialize the calendar
*/
Private void Init (){
Font font = new font ("dialog", Font. Bold, 14 );
Yearlabel. setfont (font );
Monthlabel. setfont (font );
Button_ OK .setfont (font );
Button_today.setfont (font );
// Set the year range to the next 20 years from the past 10 years of the current year to the current year.
For (INT I = now_year-10; I <= now_year + 20; I ++ ){
Yearbox. additem (I + "");
}
// Set the year drop-down list to the current year, which is in the 10th
Yearbox. setselectedindex (10 );
// Set the month interval to 12 months
For (INT I = 1; I <= 12; I ++ ){
Monthbox. additem (I + "");
}
// Set the month drop-down list to the current month
Monthbox. setselectedindex (now_month );
// Place the drop-down list box and control button panel
Jpanel panel_ym = new jpanel ();
Panel_ym.add (yearlabel );
Panel_ym.add (yearbox );
Panel_ym.add (monthlabel );
Panel_ym.add (monthbox );
Panel_ym.add (button_ OK );
Panel_ym.add (button_today );
// Add event listeners for two buttons
Button_ OK .addactionlistener (this );
Button_today.addactionlistener (this );
Jpanel panel_day = new jpanel (); // place the date panel
// Grid layout manager, with seven rows and seven columns. the horizontal and vertical directions between grids are 5
Panel_day.setlayout (New gridlayout (7,7, 3,3 ));
// Add the name of the week and put it in the panel.
For (INT I = 0; I <7; I ++ ){
Button_week [I] = new jbutton ("");
Button_week [I]. settext (week [I]);
Button_week [I]. setforeground (color. Black );
Panel_day.add (button_week [I]);
}
Button_week [0]. setforeground (color. Red );
Button_week [6]. setforeground (color. Red );
// Add the date to the Panel
For (INT I = 0; I <42; I ++ ){
Button_day [I] = new jbutton ("");
Panel_day.add (button_day [I]);
}
This. paintday (); // display the date of the current year and month
Jpanel panel_main = new jpanel (); // place the above two panels
// Border layout manager
Panel_main.setlayout (New borderlayout ());
Panel_main.add (panel_day, borderlayout. South );
Panel_main.add (panel_ym, borderlayout. North );
Getcontentpane (). Add (panel_main );
}
/** Display the date of the current year and month */
Private void paintday (){
If (this. todayflag ){
// Display the date of today
Year_int = now_year + "";
Month_int = now_month;
} Else {
// Otherwise, obtain the user's year and month from the drop-down list.
Year_int = yearbox. getselecteditem (). tostring ();
Month_int = monthbox. getselectedindex (); // The selected sequence number.
}
Int year_sel = integer. parseint (year_int)-1900; // get the year
Date firstday = new date (year_sel, month_int, 1); // construct the first day of the month
Gregoriancalendar Cal = new gregoriancalendar (); // create a calendar instance
Cal. settime (firstday );
Int days = 0; // number of days in a month
Int day_week = 0; // The number of the day of the week on the first day of a month.
// Determine the month, and set the value of day based on it. In February, determine whether it is a leap year.
If (month_int = 0 | month_int = 2 | month_int = 4 | month_int = 6 | month_int = 7
| Month_int = 9 | month_int = 11 ){
Days = 31;
} Else if (month_int = 3 | month_int = 5 | month_int = 8 | month_int = 10 ){
Days = 30;
} Else {
// January 1, February. If it is a leap year, there will be 29 days; otherwise, there will be 28 days.
If (Cal. isleapyear (year_sel )){
Days = 29;
} Else {
Days = 28;
}
}
// Getday method to obtain the day of the week
Day_week = firstday. getday ();
Int COUNT = 1;
/**
* Draw button. First, you must determine the start position of the draw button Based on the day of the selected month and the day of the week.
* Day_week is the starting position of the image to be drawn, and the buttons that do not have a value to be displayed must be left blank.
*/
For (INT I = day_week; I <day_week + days; count ++, I ++ ){
If (I % 7 = 0 | I = 6 | I = 13 | I = 20 | I = 27 | I = 34 | I = 41) {
// If it is a button on the boundary, the text is in red and indicates the weekend
If (I = day_week + now_date.getdate ()-1 ){
// Mark the same date as today in blue
Button_day [I]. setforeground (color. Blue );
Button_day [I]. settext (count + "");
} Else {
// The text in the buttons on other boundaries is in red.
Button_day [I]. setforeground (color. Red );
Button_day [I]. settext (count + "");
}
} Else {
If (I = day_week + now_date.getdate ()-1 ){
// Mark the same date as today in blue
Button_day [I]. setforeground (color. Blue );
Button_day [I]. settext (count + "");
} Else {
// The text on the button at the normal position is marked in black
Button_day [I]. setforeground (color. Black );
Button_day [I]. settext (count + "");
}
}
}
// Empty button without date value display
If (day_week = 0 ){
// If the first day is Sunday, set the text on the button in the last meter to null.
For (INT I = days; I <42; I ++ ){
Button_day [I]. settext ("");
}
} Else {
// If the first day is not Sunday, leave the button before the first day empty.
For (INT I = 0; I <day_week; I ++ ){
Button_day [I]. settext ("");
}
}
}
Public void actionreceivmed (actionevent e ){
If (E. getsource () = button_ OK ){
// If you click "View", call setday () to redraw the method.
This. todayflag = false;
This. paintday ();
} Else if (E. getsource () = button_today ){
// If you click "today", get the date of today
Todayflag = true;
Yearbox. setselectedindex (10 );
Monthbox. setselectedindex (now_month );
This. paintday ();
}
}
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Calendertrain Ct = new calendertrain ();
Ct. setdefaclocloseoperation (jframe. exit_on_close );
Ct. setvisible (true );
// CT. Show ();
}
}
I modified the GUI calendar program, which can be written as a Java Apple calendar on the web page:
Package example;
Import java. AWT. borderlayout;
Import java. AWT. color;
Import java. AWT. gridlayout;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;
Import java. util. date;
Import java. util. gregoriancalendar;
Import javax. Swing. jframe;
Import javax. Swing. jlabel;
Import javax. Swing. jpanel;
Import javax. Swing. jbutton;
Import java. AWT. Font ;;
/**
* Calendar Program
*/
Public class calendercreator extends jframe implements actionlistener {
/**
*
*/
Private Static final long serialversionuid = 1l;
// "View" and "today" buttons
Private jbutton button_left = new jbutton ("<");
Private jbutton button_right = new jbutton ("> ");
Private jlabel yearlabel;
Private jlabel monthlabel;
Private jlabel daylabel;
// Obtain the date, year, and month of the current day.
Private date now_date = new date ();
Private int now_year = fig () + 1900;
Private int now_month = now_date.getmonth ();
Private int nowday = now_date.getdate ();
// Display the date with a group of buttons. There are 7 rows and 7 columns in total, with the name of the first behavior week
Private jlabel [] label_day = new jlabel [42];
Private final string [] week = {"day", "one", "two", "three", "four", "five", "Six "};
Private jlabel [] label_week = new jlabel [7];
Private string year_int = NULL; // Save the year selected
Private int month_int = 0; // Save the month selected
/**
* Constructor
*/
Public calendercreator (){
Super ();
Year_int = integer. tostring (now_year );
Month_int = now_month;
This. settitle ("calendar"); // you can specify the title.
This. INIT ();
This. setlocation (500,300 );
This. setresizable (false); // The Panel size cannot be changed.
Pack ();
}
/**
*
* Initialize the calendar
*/
Private void Init (){
// Month and year labels
Yearlabel = new jlabel (year_int + "year ");
Monthlabel = new jlabel (month_int + 1) + "month ");
Daylabel = new jlabel (nowday + "no ");
Font font = new font ("dialog", Font. Bold, 14 );
Yearlabel. setfont (font );
Monthlabel. setfont (font );
Daylabel. setfont (font );
Button_left.setfont (font );
Button_right.setfont (font );
// Place the drop-down list box and control button panel
Jpanel panel_ym = new jpanel ();
Panel_ym.add (button_left );
Panel_ym.add (yearlabel );
Panel_ym.add (monthlabel );
Panel_ym.add (daylabel );
Panel_ym.add (button_right );
// Add event listeners for two buttons
Button_left.addactionlistener (this );
Button_right.addactionlistener (this );
Jpanel panel_day = new jpanel (); // place the date panel
// Grid layout manager, with seven rows and seven columns. the horizontal and vertical directions between grids are 5
Panel_day.setlayout (New gridlayout (7,7, 3,3 ));
// Add the name of the week and put it in the panel.
For (INT I = 0; I <7; I ++ ){
Label_week [I] = new jlabel ("");
Label_week [I]. settext (week [I]);
Label_week [I]. setforeground (color. Black );
Panel_day.add (label_week [I]);
}
Label_week [0]. setforeground (color. Red );
Label_week [6]. setforeground (color. Red );
// Add the date to the Panel
For (INT I = 0; I <42; I ++ ){
Label_day [I] = new jlabel ("");
Panel_day.add (label_day [I]);
}
This. paintday (); // display the date of the current year and month
Jpanel panel_main = new jpanel (); // place the above two panels
// Border layout manager
Panel_main.setlayout (New borderlayout ());
Panel_main.add (panel_day, borderlayout. South );
Panel_main.add (panel_ym, borderlayout. North );
Getcontentpane (). Add (panel_main );
}
/** Display the date of the current year and month */
Private void paintday (){
// Month and year labels
Yearlabel. settext (year_int + "year ");
Monthlabel. settext (month_int + 1) + "month ");
// Display the date of today
Int year_sel = integer. parseint (year_int)-1900; // get the year
Date firstday = new date (year_sel, month_int, 1); // construct the first day of the month
Gregoriancalendar Cal = new gregoriancalendar (); // create a calendar instance
Cal. settime (firstday );
Int days = 0; // number of days in a month
Int day_week = 0; // The number of the day of the week on the first day of a month.
// Determine the month, and set the value of day based on it. In February, determine whether it is a leap year.
If (month_int = 0 | month_int = 2 | month_int = 4 | month_int = 6 | month_int = 7
| Month_int = 9 | month_int = 11 ){
Days = 31;
} Else if (month_int = 3 | month_int = 5 | month_int = 8 | month_int = 10 ){
Days = 30;
} Else {
// January 1, February. If it is a leap year, there will be 29 days; otherwise, there will be 28 days.
If (Cal. isleapyear (year_sel )){
Days = 29;
} Else {
Days = 28;
}
}
// Getday method to obtain the day of the week
Day_week = firstday. getday ();
Int COUNT = 1;
/**
* Draw button. First, you must determine the start position of the draw button Based on the day of the selected month and the day of the week.
* Day_week is the starting position of the image to be drawn, and the buttons that do not have a value to be displayed must be left blank.
*/
For (INT I = day_week; I <day_week + days; count ++, I ++ ){
If (I % 7 = 0 | I = 6 | I = 13 | I = 20 | I = 27 | I = 34 | I = 41) {
// If it is a button on the boundary, the text is in red and indicates the weekend
If (I = day_week + now_date.getdate ()-1 ){
// Mark the same date as today in blue
Label_day [I]. setforeground (color. Blue );
Label_day [I]. settext (count + "");
} Else {
// The text in the buttons on other boundaries is in red.
Label_day [I]. setforeground (color. Red );
Label_day [I]. settext (count + "");
}
} Else {
If (I = day_week + now_date.getdate ()-1 ){
// Mark the same date as today in blue
Label_day [I]. setforeground (color. Blue );
Label_day [I]. settext (count + "");
} Else {
// The text on the button at the normal position is marked in black
Label_day [I]. setforeground (color. Black );
Label_day [I]. settext (count + "");
}
}
}
// Empty button without date value display
If (day_week = 0 ){
// If the first day is Sunday, set the text on the button in the last meter to null.
For (INT I = days; I <42; I ++ ){
Label_day [I]. settext ("");
}
} Else {
// If the first day is not Sunday, leave the button before the first day empty.
For (INT I = 0; I <day_week; I ++ ){
Label_day [I]. settext ("");
}
}
}
Public void actionreceivmed (actionevent e ){
If (E. getsource () = button_left ){
// If you click "View", call setday () to redraw the method.
Month_int = month_int-1;
If (month_int <0 ){
Month_int = 11;
Year_int = integer. tostring (integer. parseint (year_int)-1 );
}
This. paintday ();
} Else if (E. getsource () = button_right ){
// If you click "today", get the date of today
Month_int = month_int + 1;
If (month_int> 11 ){
Month_int = 0;
Year_int = integer. tostring (integer. parseint (year_int) + 1 );
}
This. paintday ();
}
}
Public static void main (string [] ARGs ){
Calendercreator Ct = new calendercreator ();
Ct. setdefaclocloseoperation (jframe. exit_on_close );
Ct. setvisible (true );
}
}