The calendar displays the java code for reading the input year/year.

Source: Internet
Author: User

The calendar displays the java code for reading the input year/year.

This article mainly introduces the java code for reading the input year/year from the calendar display. If you need it, refer to it.

 

The Code is as follows:

Import java. awt .*;

Import java. awt. event .*;

Import java. util. Calendar;

 

Import javax. swing. JOptionPane;

 

Class CalendarBean {// defines a calendar class

String day []; // Number of days Array

Int year = 2011, month = 9; // specify an initial year.

 

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 () {// obtain the calendar

String a [] = new String [42]; // defines an array of strings.

Calendar = Calendar. getInstance ();

Calendar. set (year, month-1, 1 );

Int day of the week = Calendar. get (Calendar. DAY_OF_WEEK)-1;

Int day = 0;

If (month = 1 | month = 3 | month = 5 | month = 7 | month = 8 | month = 10 | month = = 12) {

Day = 31;

}

If (month = 4 | month = 6 | month = 9 | month = 11 ){

Day = 30;

}

If (month = 2 ){

If (year % 4 = 0) & (year % 100! = 0) | (year % 400 = 0 )){

Day = 29;

} Else {

Day = 28;

}

}

For (int I = day of the week, n = 1; I <day of the week + day; I ++ ){

A [I] = String. valueOf (n );

N ++;

}

Return;

}

}

 

Class CalendarFrame extends Frame implements ActionListener {

Label labelDay [] = new Label [42]; // an array of days used to output the calendar

Button titleName [] = new Button [7]; // keys from Monday to Sunday

String name [] = {"day", "1", "2", "3", "4", "5", "6 "};

TextField text1, text2; // defines the input year and month

Button nextMonth, previusmonth, Enter; // next month, last month, OK

Label lab1, lab2, lab3; // Several text strings

Int year = 2012, month = 9;

CalendarBean calendar; // the maximum value.

Label showMessage = new Label ("", Label. CENTER); // defines a label used to display the current year and month. The Label should be CENTER.

 

Public CalendarFrame () {// form class

Panel pCenter = new Panel (); // set

PCenter. setLayout (new GridLayout (7, 7 ));

For (int I = 0; I <7; I ++) {// Add text to the button from Sunday to Saturday

TitleName [I] = new Button (name [I]);

PCenter. add (titleName [I]);

}

For (int I = 0; I <42; I ++ ){

LabelDay [I] = new Label ("", Label. CENTER );

PCenter. add (labelDay [I]);

}

Calendar = new CalendarBean ();

Calendar. setYear (year); // sets the year.

Calendar. setMonth (month );

String day [] = calendar. getCalendar (); // set the day

For (int I = 0; I <42; I ++) {// Add and display calendar days to the calendar position loop

LabelDay [I]. setText (day [I]);

}

Lab1 = new Label ("Enter the date"); // call a method to create an object

Lab2 = new Label ("year ");

Lab3 = new Label ("month ");

Enter = new Button ("OK ");

Text1 = new TextField (10 );

Text2 = new TextField (5 );

NextMonth = new Button ("Next Month ");

Previusmonth = new Button ("last month ");

Enter. addActionListener (this); // Add the specified action listener

NextMonth. addActionListener (this );

Previusmonth. addActionListener (this );

Panel pNorth = new Panel (), pSouth = new Panel ();

PNorth. add (lab1 );

PNorth. add (lab2 );

PNorth. add (text1 );

PNorth. add (lab3 );

PNorth. add (text2 );

PNorth. add (Enter );

PNorth. add (previusmonth );

PNorth. add (nextMonth );

PSouth. add (showMessage );

ShowMessage. setText ("calendar:" + calendar. getYear () + "year" + calendar. getMonth () + "month ");

ScrollPane scrollPane = new ScrollPane (); // container class that implements automatic horizontal and/or vertical scrolling of a single sub-component

ScrollPane. add (pCenter );

Add (scrollPane, BorderLayout. CENTER );

Add (pNorth, BorderLayout. NORTH );

Add (pSouth, BorderLayout. SOUTH );

}

 

Public void actionreceivmed (ActionEvent e ){

If (e. getSource () = nextMonth) {// if the obtained operation is in the current month

Month = 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 () = previusmonth ){

Month = month-1;

If (month <1)

Month = 12;

Calendar. setMonth (month );

String day [] = calendar. getCalendar ();

For (int I = 0; I <42; I ++ ){

LabelDay [I]. setText (day [I]);

}

} Else {

String yea = text1.getText ();

String mon = text2.getText ();

Try {

Year = Integer. parseInt (yea); // convert string to int type

Month = Integer. parseInt (mon );

If (month> 12 | month <1 | year <1) {// handle incorrect input

JOptionPane. showMessageDialog (null, "enter the correct month or month ");

Return;

} Else {

Calendar. setYear (year );

Calendar. setMonth (month );

}

String day [] = calendar. getCalendar ();

For (int I = 0; I <42; I ++ ){

LabelDay [I]. setText (day [I]);

}

} Catch (NumberFormatException ee ){

JOptionPane. showMessageDialog (null, "enter the correct year and month ");

}

}

ShowMessage. setText ("calendar:" + calendar. getYear () + "year" + calendar. getMonth () + "month ");

}

 

}

 

Public class CalendarMainClass {

Public static void main (String args []) {

CalendarFrame frame = new CalendarFrame ();

Frame. setTitle ("calendar ");

Frame. setBounds (300,200,500,300 );

Frame. setVisible (true); // display window

Frame. validate (); // make it take effect

Frame. addWindowListener (new java. awt. event. WindowAdapter () {// click the cross sign to close the frame program

Public void windowClosing (java. awt. event. javaswevent e ){

System. exit (0 );

}

});

}

}

 

Related Article

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.