JAVA----Calendar Source code

Source: Internet
Author: User

1: First create a Calendarbean class:

Code:

  1. Import Java.util.Calendar;
  2. Public class Calendarbean
  3. {
  4. String day[];
  5. int year=2005,month=0;
  6. Public void setyear (int year)
  7. {
  8. This.year=year;
  9. }
  10. public int GetYear ()
  11. {
  12. Return year;
  13. }
  14. Public void setmonth (int month)
  15. {
  16. This.month=month;
  17. }
  18. public int GetMonth ()
  19. {
  20. Return month;
  21. }
  22. Public string[] Getcalendar ()
  23. {
  24. String a[]=New string[42];
  25. Calendar date=calendar.getinstance ();
  26. Date.set (year,month-1,1);
  27. int Week=date.get (Calendar.day_of_week)-1;
  28. int day=0;
  29. Judging the big month
  30. if (month==1| | month==3| | month==5| | month==7
  31. || month==8| |month==10| | month==)
  32. {
  33. day=31;
  34. }
  35. Judging Xiao Yue
  36. if (month==4| | month==6| | month==9| | month==)
  37. {
  38. day=30;
  39. }
  40. Judging common year and leap years
  41. if (month==2)
  42. {
  43. if (((year%4==0) && (year%100!=0)) | | (year%400==0))
  44. {
  45. day=29;
  46. }
  47. Else
  48. {
  49. day=28;
  50. }
  51. }
  52. for (int i=week,n=1;i<week+day;i++)
  53. {
  54. A[i]=string.valueof (n);
  55. n++;
  56. }
  57. return A;
  58. }
  59. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.