The calendar shows the Java code for the year in which the input was read _java

Source: Internet
Author: User
Tags gettext

Copy Code code as follows:

Import java.awt.*;
Import java.awt.event.*;
Import Java.util.Calendar;

Import Javax.swing.JOptionPane;

Class Calendarbean {//define a calendar class
String day[]; Array of Days
int year = month = 9; Given an initial date

public void setyear (int year) {
This.year = year;
}

public int getyear () {
return to year;
}

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 weeks = Calendar. Get (Calendar.day_of_week)-1;
int day = 0;
if (month = 1 | | month = 3 | | | month = 5 | | month = 7 | | month = 8 | | month = | | 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 = Days of the week, n = 1; i < week + day; i++) {
A[i] = string.valueof (n);
n++;
}
return A;
}
}

Class Calendarframe extends Frame implements ActionListener {
Label labelday[] = new LABEL[42]; An array of days to output the calendar
Button titlename[] = new BUTTON[7]; Keys from Monday to Sunday
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, OK
Label lab1, LAB2, lab3; A string of several literal types
int year = month = 9;
Calendarbean Calendar; Top One amount
Label ShowMessage = new Label ("", label.center); Defining a label to display the current year indicates 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++) {//For button Sunday to Saturday add display text
Titlename[i] = new Button (Name[i]);
Pcenter.add (Titlename[i]);
}
for (int i = 0; i < i++) {
Labelday[i] = new Label ("", label.center);
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++) {//Add a display calendar day to the calendar location loop
Labelday[i].settext (Day[i]);
}
LAB1 = new Label ("Please enter Date"); Call 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 that implements automatic horizontal and/or vertical scrolling of a single subassembly
Scrollpane.add (Pcenter);
Add (ScrollPane, borderlayout.center);
Add (Pnorth, Borderlayout.north);
Add (Psouth, Borderlayout.south);
}

public void actionperformed (ActionEvent e) {
if (e.getsource () = = Nextmonth) {//If the operation is obtained next month's
Month = month + 1;
if (Month > 12)
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 = 12;
Calendar.setmonth (month);
String day[] = Calendar.getcalendar ();
for (int i = 0; i < 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 > | | Month < 1 | | Year < 1) {//Error input processing
Joptionpane.showmessagedialog (NULL, "Please enter the correct month or month");
Return
} else {
Calendar.setyear (year);
Calendar.setmonth (month);
}
String day[] = Calendar.getcalendar ();
for (int i = 0; i < i++) {
Labelday[i].settext (Day[i]);
}
catch (numberformatexception ee) {
Joptionpane.showmessagedialog (NULL, "Please 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 effective
Frame.addwindowlistener (New Java.awt.event.WindowAdapter () {//Click on the fork to close the frame program
public void windowclosing (Java.awt.event.WindowEvent 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.