The calendar shows the Java code that reads out the entered year

Source: Internet
Author: User
Tags gettext

  This article mainly introduced the calendar display read out the input of the Java code, the need for friends can refer to the

  Code 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 =, month = 9;////Given an initial date    public void setyear (int year) {    this.year = year;  }    public int getyear () {  R Eturn year; &NBSP}    public void setmonth (int month) {  This.month = month;  }    public int getmonth () {  return month  }    public string[] Getcalendar () {//Get calendar   String a[] = new STRING[42]; Defines a string array   Calendar calendar = calendar.getinstance ();    Calendar. Set (year, month-1, 1);   INT week = calendar. Get (Calendar.day_of_week)-1;   int day = 0;   if (month = 1 | | month = 3 | | | month = 5 | | month = 7 | | month = 8 | | month = = | | month = = =   &nbs P;day = 31;  }   if (month = 4 | | month = 6 | | month = 9 | | month = 11 {   day =  }   if (month = 2) {   if ((year% 4 = 0) && (year% 100!= 0)) | | (Year% 400 = 0)) {    day = +   &NBSP} else {    day =   &NBSP}  }   for (int i = Days of the week, n = 1; I < week + day; i++) {   a[i] = string.valueof (n);    n++;  }   return A; &NBSP}   Class Calendarframe extends Frame implements ActionListener { label labelday[] = new LABEL[42]; To output the day array of the calendar  button titlename[] = new BUTTON[7]; Monday to Sunday key  string name[] = {"Day", "one", "two", "three", "four", "five", "six"};  textfield Text1, Text2; Define the year and month entered  button nextmonth, previousmonth, enter; Next month, last month, determine  label lab1, LAB2, lab3; String of several literal types  int year = 9;  calendarbean Calendar; Top one quantity  label ShowMessage = new Label ("", label.center);   defines a label to display the current year indicating that the label should be centered.    public Calendarframe () {   //form class  panel pcenter = new Panel ();  //Mosaic   Pcenter.setlayout (New GridLayout (7, 7));   for (int i = 0; i < 7; i++) {     //to button Sunday to Saturday add display text    titlename[i] = new button (n Ame[i]);    pcenter.add (Titlename[i]);  }   for (int i = 0; i < i++) {         labelday[i] = new Label ("", label.cent ER);    pcenter.add (Labelday[i]);  }   Calendar = new Calendarbean ();   Calendar.setyear (year);    /Set year   calendar.setmonth (month);   String day[] = Calendar.getcalendar ();  //Set day   for (int i = 0; i < i++) {   //Cycle add Display calendar day    labelday[i].settext to calendar location (day[i ]);  }   LAB1 = new Label ("Please enter Date");  //calls a method    new 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");   Previousmonth = New button ("Last month");   Enter.addactionlistener (this);  //Add the specified action listener   Nextmonth.addactionlistener (this);   Previousmonth.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 (Previousmonth);   Pnorth.add (Nextmonth);   Psouth.add (showmessage);   Showmessage.settext ("calendar:" + calendar.getyear () + "year" + calendar.getmonth () + "month");   ScrollPane ScrollPane = new ScrollPane ();  //Container class   Scrollpane.add (Pcenter);    Add (ScrollPane, borderlayout.center) that implement automatic horizontal and/or vertical scrolling of a single subassembly;   Add (Pnorth, Borderlayout.north);   Add (Psouth, Borderlayout.south); &NBSP}    public void actionperformed (ActionEvent e) {  if (e.getsource () = = Nextmonth) {//If the operation obtained is next month's    month = month + 1; &nbsp  if (Month >)     month = 1;    calendar.setmonth (month);    string day[] = Calendar.getcalendar ();    for (int i = 0; i < i++) {    Labelday[i].settext (Day[i]);    }  } else if (E.getsource () = = Previousmonth) {   month = month-1    if (Month < 1)     month = the    calendar.setmonth ( month);    string day[] = Calendar.getcalendar ();    for (int i = 0; i < i++) {    Labelday[i].settext (Day[i]);    }  } else {& nbsp  string yea = text1.gettext ();    string Mon = Text2.gettext ();    try {    year = Integer.parseint (yea);//convert string to int type       month = Integer.parsei NT (Mon);     if (Month > | | Month < 1 | | Year < 1) { //error input processing      joptionpane.showmess Agedialog (NULL, "Please enter the correct month or month");        return;    } else {     calendar.setyear (year);      calendar.setmonth (month);   &N Bsp }     String day[] = Calendar.getcalendar ();     for (int i = 0; i < i++) {     labelday[i].settext (Day[i]);    }   &N BSP;} catch (numberformatexception ee) {    Joptionpane.showmessagedialog (null, "Please enter the correct year and month");    } & nbsp }   Showmessage.settext ("calendar:" + calendar.getyear () + "year" + calendar.getmonth () + "month"); &NBSP}  }   public class Calendarmainclass { public static void Main (String args[]) {  CALENDARFRA Me frame = new Calendarframe ();   Frame.settitle ("Calendar");   Frame.setbounds (300, 200, 500, 300);   Frame.setvisible (TRUE);  //Display window   frame.validate (); Make effective   Frame.addwindowlistener (new Java.awt.event.WindowAdapter () { //Click on Fork to close frame program    public void Windowclosing (Java.awt.event.WindowEvent e) {&NBSp   System.exit (0);     }  }); &NBSP}}  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.