Write a small calendar of their own, run the program is KE automatic positioning to the current month and the day, when the Next button will be positioned to the next month of the day, that is, the number of days will not change. When you click on a day in a one-month period, the following times will change at any time.
ImportJava.util.Calendar; Public class calendarmainclass { Public Static void Main(String args[]) {Calendarframe frame=NewCalendarframe (); Frame.setbounds ( -, -, the, -); Frame.setvisible (true); Frame. Setyearandmonth ((Calendar.getinstance ()). Get (Calendar.year), (Calendar.getinstance ()). Get (Calendar.month) +1); }}Importjava.util.*;Importjava.awt.*;Importjava.awt.event.*;Importjavax.swing.*;Importjavax.swing.border.*; Public class calendarframe extends JFrame implements ActionListener , mouselistener{ /** * */ Private Static Final LongSerialversionuid =1L JLabel labelday[]=Newjlabel[ the]; JButton titlename[]=Newjbutton[7]; JLabel labelcom=NewJLabel ();//Track the selected dateString name[]={"Day","One","Two","three","Four","Five","Six"}; JButton Nextmonth,previousmonth; Calendarbean Calendar; JLabel showmessage=NewJLabel ("", Jlabel.center);intYear= ., month=2; Public Calendarframe() {JPanel pcenter=NewJPanel (); Pcenter.setbackground (Color.White); Pcenter.setlayout (NewGridLayout (7,7));//Set the layout of the Pcenter to a gridlayout layout of 7 rows and 7 columns. for(intI=0;i<7; i++) {titlename[i]=NewJButton (Name[i]); Titlename[i].setborder (NewSoftbevelborder (bevelborder.raised)); Pcenter.add (Titlename[i]); } for(intI=0;i< the; i++) {labelday[i]=NewJLabel ("", Jlabel.center);//labelday[i].setborder (New Etchedborder (etchedborder.raised));Labelday[i].setborder (Borderfactory.createlineborder (Color.orange,1)); Pcenter.add (Labelday[i]);//pcenter Add component Labelday[i]. } calendar=NewCalendarbean (); Nextmonth=NewJButton ("Next month"); Previousmonth=NewJButton ("Last month"); Nextmonth.addactionlistener ( This); Previousmonth.addactionlistener ( This); JPanel pnorth=NewJPanel (), psouth=NewJPanel (); Pcenter.setbackground (Color.White); Pnorth.setbackground (Color.White); Psouth.setbackground (Color.White); Pnorth.add (Previousmonth); Pnorth.add (Nextmonth); Psouth.add (ShowMessage); Add (Pcenter,borderlayout.center); Add (Pnorth,borderlayout.north);//Window add Pnorth in the North areaAdd (Psouth, Borderlayout.south);//Window added Psouth in the South area. //setyearandmonth (year,month);Setdefaultcloseoperation (Dispose_on_close); } Public void Setyearandmonth(intYintm) {year=y; Month=m; Calendar.setyear (y); Calendar.setmonth (m); Calendar.setday ((Calendar.getinstance ()). Get (Calendar.day_of_month)); String Day[]=calendar.getcalendar (); for(intI=0;i< the; i++) {Labelday[i].settext (day[i]); Labelday[i].addmouselistener ( This);//monitor for each label if(day[i]!=NULL&&day[i].equals (String.valueof (Calendar.getday ())))//system date is yellow{Labelday[i].setopaque (true);//Label setting is opaque to set colorLabelday[i].setbackground (Color.orange); Labelcom= Labelday[i];//Lighted labels}} showmessage.settext ("Calendar:"+calendar.getyear () +"Year"+ calendar.getmonth () +"Month"+ calendar.getday () +"Day"); } Public void actionperformed(ActionEvent e) {if(E.getsource () ==nextmonth) {month=month+1;if(month> A) {month=1; year=year+1; } calendar.setmonth (month); Calendar.setyear (year); String Day[]=calendar.getcalendar (); for(intI=0;i< the; i++) {Labelday[i].setopaque (false); } for(intI=0;i< the; i++) {Labelday[i].settext (day[i]); Labelday[i].addmouselistener ( This);if(day[i]!=NULL&&day[i].equals (String.valueof (Calendar.getday ()))) {Labelday[i].setopaque (true); Labelday[i].setbackground (Color.orange); Labelcom= Labelday[i]; } } }Else if(E.getsource () ==previousmonth) {month=month-1;if(month<1) {month= A; year=year-1; } calendar.setmonth (month); Calendar.setyear (year); String Day[]=calendar.getcalendar (); for(intI=0;i< the; i++) {Labelday[i].setopaque (false); } for(intI=0;i< the; i++) {Labelday[i].settext (day[i]); Labelday[i].addmouselistener ( This);if(day[i]!=NULL&&day[i].equals (String.valueof (Calendar.getday ()))) {Labelday[i].setopaque (true); Labelday[i].setbackground (Color.orange); Labelcom= Labelday[i]; }}} Showmessage.settext ("Calendar:"+calendar.getyear () +"Year"+ calendar.getmonth () +"Month"+ calendar.getday () +"Day"); }@Override Public void mouseclicked(MouseEvent e) {//TODO auto-generated method stub}@Override Public void mousepressed(MouseEvent e) {JLabel i= (JLabel) E.getsource ();if(I.gettext ()! =NULL) {Labelcom.setopaque (false); I.setopaque (true); I.setbackground (Color.orange); Showmessage.settext ("Calendar:"+calendar.getyear () +"Year"+ calendar.getmonth () +"Month"+integer.parseint (I.gettext ()) +"Day"); Labelcom=i;//Refresh Interface for(intj=0;j< the; j + +) {labelday[j].repaint (); } } }@Override Public void mousereleased(MouseEvent e) {//TODO auto-generated method stub}@Override Public void mouseentered(MouseEvent e) {//TODO auto-generated method stub}@Override Public void mouseexited(MouseEvent e) {//TODO auto-generated method stub}}ImportJava.util.Calendar; Public class calendarbean {String day[];intYear=2005, month=0;intdday=0; Public void setyear(intYear) { This. year=year; } Public int GetYear() {returnYear } Public void setmonth(intMonth) { This. Month=month; } Public int GetMonth() {returnMonth } Public void Setday(intDDay) { This. Dday=dday; } Public int GetDay() {returnDDay; } PublicString[]Getcalendar() {String a[]=Newstring[ the]; Calendar =calendar.getinstance ();//Get system timeCalendar. Set (year,month-1,1);//Set specified time intDay of the week = Calendar. Get (Calendar.day_of_week)-1;intday=0;if(month==1|| month==3|| month==5|| month==7|| month==8|| month==Ten|| month== A) day= to;if(month==4|| month==6|| month==9|| month== One) day= -;if(month==2) {if(((year%4==0) && (year% -!=0))|| (year% -==0)) day= in;Elseday= -; } for(intI= Week, n=1;i< weeks +day;i++) {a[i]=string.valueof (n); n++; }returnA } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Small Calendar