The calendar shows the Java code that reads the date of the input

  • 2020-04-01 02:36:47
  • OfStack


import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import javax.swing.JOptionPane;
class CalendarBean {  //Define a calendar class
 String day[]; //Number of array
 int year = 2011, month = 9; //Given a starting date
 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() { //Get a calendar
  String a[] = new String[42]; //Defines an array of strings
  Calendar  The calendar  = Calendar.getInstance(); 
   The calendar .set(year, month - 1, 1);
  int  What day  =  The 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 =  What day , n = 1; i <  What day  + 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 used to output the calendar
 Button titleName[] = new Button[7]; //Monday to Sunday button
 String name[] = { " day ", " one ", " two ", " three ", " four ", " five ", " six " };
 TextField text1, text2; //Define the year and month of the input
 Button nextMonth, previousMonth, Enter; //Next month, last month, sure
 Label lab1, lab2, lab3; //A string of several text types
 int year = 2012, month = 9;
 CalendarBean calendar; //A quantity
 Label showMessage = new Label("", Label.CENTER); //    Define a label that displays the current date to indicate that the label should be centered.
 public CalendarFrame() {    //The form class
  Panel pCenter = new Panel();  //Mosaic
  pCenter.setLayout(new GridLayout(7, 7));
  for (int i = 0; i < 7; i++) {      //Add display text to the Sunday through Saturday buttons
   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);     //Set in
  calendar.setMonth(month);
  String day[] = calendar.getCalendar();  //Set the day
  for (int i = 0; i < 42; i++) {     //Add a display calendar day to the calendar location loop
   labelDay[i].setText(day[i]);
  }
  lab1 = new Label(" Please enter the date ");  //Calling a method & NBSP;     New object
  lab2 = new Label(" year ");
  lab3 = new Label(" in ");
  Enter = new Button(" determine ");
  text1 = new TextField(10);
  text2 = new TextField(5);
  nextMonth = new Button(" Next month, ");
  previousMonth = new Button(" Last month, ");
  Enter.addActionListener(this);  //Adds 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() + " years " + calendar.getMonth() + " month ");
  ScrollPane scrollPane = new ScrollPane();  //Container classes that implement automatic horizontal and/or vertical scrolling of individual child components
  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
   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() == previousMonth) {
   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 a string to an int
    month = Integer.parseInt(mon);
    if (month > 12 || month < 1 || year < 1) {  //Handling of incorrect input
     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 < 42; i++) {
     labelDay[i].setText(day[i]);
    }
   } catch (NumberFormatException ee) {
    JOptionPane.showMessageDialog(null, " Please enter the correct year and month ");
   }
  }
  showMessage.setText(" Calendar: " + calendar.getYear() + " years " + calendar.getMonth() + " month ");
 }
}
public class CalendarMainClass {
 public static void main(String args[]) {
  CalendarFrame frame = new CalendarFrame();
  frame.setTitle(" The calendar ");
  frame.setBounds(300, 200, 500, 300);
  frame.setVisible(true);  //Display window
  frame.validate(); //To take effect
  frame.addWindowListener(new java.awt.event.WindowAdapter() {  //Click the cross to close the frame program
   public void windowClosing(java.awt.event.WindowEvent e) {
    System.exit(0); 
   }
  });
 }
}


Related articles: