Reply: A calendar Program (basically every line has a comment) Import java. AWT .*; Import java. AWT. event .*; Import javax. Swing .*; Import java. util .*;Public class calendertrain extends jframe implements actionlistener { Jcombobox month = new jcombobox (); // month drop-down list box Jcombobox year = new jcombobox (); // year drop-down list box Jlabel year_l = new jlabel ("Year:"); // define a label Jlabel month_l = new jlabel ("month:"); // define a label Date now_date = new date (); // obtain the date of today Jbutton [] button_day = new jbutton [49]; // defines an array to store dates Jbutton button_ OK = new jbutton ("OK"); // specifies the actual date. Jbutton button_today = new jbutton ("today"); // display the today button Int now_year = now_date.getyear () + 1900; // obtain the year value. Int now_month = now_date.getmonth (); // obtain the month value (current month-1) String year_int = NULL; // YEAR OF STORAGE Int month_int; // month Jpanel pane_ym = new jpanel (); // place the drop-down list box and control button panel Jpanel pane_day = new jpanel (); // place the date panel Jpanel pane_parent = new jpanel (); // place the above two panels // Define a method to draw a panel Public calendertrain (){ Super ("calender! "); // Set the title of the Panel // --- The following lines exit the program when the Panel is closed: Setdefaclocloseoperation (dispose_on_close ); Addwindowlistener (New windowadapter (){ Public void windowclose (windowevent e ){ System. Exit (0 ); } }); //--- Setresizable (false); // The Panel size cannot be changed. // Set the year and month /* The year range is the next 20 years from the past 10 years of the current year to the current year. * The month is normal. 1 ?? December */ For (INT I = now_year-10; I <= now_year + 20; I ++ ){ Year. additem (I + ""); } For (INT I = 1; I <13; I ++ ){ Month. additem (I + ""); } Year. setselectedindex (10); // set the year drop-down list to the current year. Pane_ym.add (year_l); // Add the year label Pane_ym.add (year); // Add the year drop-down list box Month. setselectedindex (now_month); // you can specify the month drop-down list as the current month. Pane_ym.add (month_l); // Add the month tag Pane_ym.add (month); // Add a month drop-down list box Pane_ym.add (button_ OK); // Add the OK button Pane_ym.add (button_today); // Add the "Today" button Button_ OK .addactionlistener (this); // click the OK button to add a listener event. Button_today.addactionlistener (this); // click "today" to add a listener event // The End of the year/month setting // Initialize the date button and draw it Pane_day.setlayout (New gridlayout (7, 7, 10, 10 )); For (INT I = 0; I <49; I ++ ){ Button_day [I] = new jbutton (""); Pane_day.add (button_day [I]); } This. setday (); // call the setday () method Pane_parent.setlayout (New borderlayout (); // sets the layout manager. Setcontentpane (pane_day ); Setcontentpane (pane_ym ); Pane_parent.add (pane_day, borderlayout. South ); Pane_parent.add (pane_ym, borderlayout. North ); Setcontentpane (pane_parent ); Pack (); Show (); } Void setday (){ Year_int = year. getselecteditem (). tostring (); Month_int = month. getselectedindex (); Int year_sel = integer. parseint (year_int)-1900; // obtain the year value. Date dt = new date (year_sel, month_int, 1); // construct a date Gregoriancalendar Cal = new gregoriancalendar (); // create a calendar instance Cal. settime (DT ); String week [] = {"sun", "mon", "Tue", "wed", "Thur", "fri", "sat "}; Int day = 0; // the number of days in a month Int day_week = 0; // used to store the values of the day of the week on the first day of a month. // -- Add the week to the first seven buttons For (INT I = 0; I <7; I ++ ){ Button_day [I]. settext (week [I]); } //-- /* Determine the month, and set the value of day based on it. * In February, you must 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 ){ Day = 31; } Else if ( Month_int = 3 | Month_int = 5 | Month_int = 8 | Month_int = 10 ){ Day = 30; } Else { If (Cal. isleapyear (year_sel )){ Day = 29; } Else { Day = 28; } } Day_week = 7 + dt. getday (); Int COUNT = 1; /* Draw button * Here, we first need to determine the start position of the 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. * The buttons without numeric values should be left blank. */ For (INT I = day_week; I <day_week + day; count ++, I ++ ){ If (I % 7 = 0 | I = 13 | I = 20 | I = 27 | I = 48 | I = 34 | I = 41 ){ Button_day [I]. setforeground (color. Red ); Button_day [I]. settext (count + ""); } Else { Button_day [I]. settext (count + ""); } } // -- Empty button without date value display If (day_week = 0 ){ For (INT I = day; I <49; I ++ ){ Button_day [I]. settext (""); } } Else { // Set the button in front of the first day to null For (INT I = 7; I <day_week; I ++ ){ Button_day [I]. settext (""); } // The button after the last day is blank For (INT I = day_week + day; I <49; I ++ ){ Button_day [I]. settext (""); } } } Public void actionreceivmed (actionevent e ){ If (E. getsource () = button_ OK ){ This. setday (); // if you click OK, call setday () to redraw the method. } Else if (E. getsource () = button_today ){ New calendertrain (); // if you click the today button, get the date of today } } Public static void main (string [] ARGs ){ Calendertrain Ct = new calendertrain (); } } |