A simple Java calendar program

Source: Internet
Author: User

/** The following is the code program for the calendar


has a question reply ycj@18e.net


**/


//calendertrain.java


package com.swing;


import java.awt.*;


import java.awt.event.*;


import javax.swing.*;


import java.util.*;


public class Calendertrain extends JFrame implements ActionListener {


JComboBox Month = new JComboBox (); Month Drop-down list box


JComboBox year = new JComboBox (); Year Drop-down list box


JLabel year_l = new JLabel ("Year::"); Define label


JLabel month_l = new JLabel ("Month::"); Define label


Date now_date = new Date (); Get today's date


jbutton[] Button_day = new jbutton[49]; Defines an array for storing dates


JButton BUTTON_OK = new JButton ("OK"); Realistic selection Date


JButton button_today = new JButton ("Today"); Show today's button


int now_year = now_date.getyear () + 1900; Get year value


int now_month = Now_date.getmonth (); Get month value (current month-1)


Boolean bool = false;


String year_int = null; Storage year


int month_int; Store month


JPanel pane_ym = new JPanel (); Place Drop-down list box and control button panel


JPanel pane_day = new JPanel (); Drop Date Panel


JPanel pane_parent = new JPanel (); Place above two panels


//Define method drawing panel


public Calendertrain () {


super ("calender!"); Set the panel to title


//---The following exercise to close the panel when the exit program


setdefaultcloseoperation (dispose_on_close);


Addwindowlistener (New Windowadapter () {


public void Windowclose (WindowEvent e) {


System.out.print ("CLOSING the WIN");


system.exit (0);


}


});


//---


setresizable (FALSE); The size of the panel cannot be changed


//Set Month


/* Year range is the current year's last 10 years to the current year of the next 20 years


* Month normal 1?? December


*/


for (int i = now_year-10 I <= now_year + i++) {


Year.additem (i + "");


}


for (int i = 1; i < i++) {


Month.additem (i + "");


}


Year.setselectedindex (10); Set year Drop-down list for current year


Pane_ym.add (year_l); Add year label


Pane_ym.add (year); Add year drop-down list box


Month.setselectedindex (Now_month); Set month Drop-down list for current month


Pane_ym.add (month_l); Add Month label


Pane_ym.add (Month); Add month drop-down list box


Pane_ym.add (BUTTON_OK); Add OK button


Pane_ym.add (Button_today); Add "Today" button


Button_ok.addactionlistener (this); OK button to add listener event


Button_today.addactionlistener (this); "Today" button to add a listener event


//month setting end


//Initialize Date button and draw


pane_day.setlayout (New GridLayout (7, 7, 10, 10));


for (int i = 0; i < i++) {


Button_day[i] = new JButton ("");


Pane_day.add (Button_day[i]);


}


This.setday (); Call the Setday () method


pane_parent.setlayout (New BorderLayout ()); Set Layout manager


Setcontentpane (Pane_day);


Setcontentpane (PANE_YM);


Pane_parent.add (Pane_day, Borderlayout.south);


Pane_parent.add (Pane_ym, Borderlayout.north);


Setcontentpane (pane_parent);


pack ();


Show ();


}


void Setday () {


if (bool) {


Year_int = Now_year + "";


month_int = Now_month;


} else {


year_int = Year.getselecteditem (). toString ();


month_int = Month.getselectedindex ();


}


int year_sel = Integer.parseint (year_int)-1900; Get year value


Date dt = new Date (Year_sel, Month_int, 1); Construct a date


GregorianCalendar cal = new GregorianCalendar (); Create a Calendar instance


cal.settime (DT);


String week[] = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"};


int day = 0; Days in which a month is stored


int day_week = 0; The number that is used to store the first day of the month is the
of the week.

//--adds the week to the top 7 buttons


for (int i = 0; i < 7; i++) {


Button_day[i].settext (Week[i]);


}


//--


/* Judgment is a few months, according to which you set day's value


* One of the February to determine whether it is a leap year


*/


if (month_int = 0


|| Month_int = 2


|| Month_int = 4


|| Month_int = 6


|| Month_int = 7


|| Month_int = 9


|| Month_int = = 11) {


day = 31;


} else if (


Month_int = = 3


|| Month_int = 5


|| Month_int = 8


|| Month_int = = 10) {


Day = 30;


} else {


if (Cal.isleapyear (Year_sel)) {


day = 29;


} else {


day = 28;


}


}


Day_week = 7 + dt.getday ();


int count = 1;


/* Draw button


* Here we first want to determine the starting position of the button by the first day of the month selected


* Where Day_week is the starting position we want to draw


* Empty
for buttons that don't have a value to display

*/


for (int i = Day_week i < Day_week + day; count++, i++) {


if (i% 7 = 0


|| i = = 13


|| i = = 20


|| i = = 27


|| i = = 48


|| i = = 34


|| i = = 41) {


if (i = = Day_week + now_date.getdate ()-1) {


Button_day[i].setforeground (Color.Blue);


Button_day[i].settext (Count + "");


} else {


Button_day[i].setforeground (color.red);


Button_day[i].settext (Count + "");


}


} else {


if (i = = Day_week + now_date.getdate ()-1) {


Button_day[i].setforeground (Color.Blue);


Button_day[i].settext (Count + "");


} else {


Button_day[i].setforeground (Color.Black);


Button_day[i].settext (Count + "");


}


}


}


//--for a button that has no date value display


if (Day_week = = 0) {


for (int i = day; I < i++) {


Button_day[i].settext ("");


}


} else {


//First day front button to empty


for (int i = 7; i < Day_week; i++) {


Button_day[i].settext ("");


}//The button behind the last day to empty


for (int i = Day_week + day; i < i++) {


Button_day[i].settext ("");


}


}


}


public void actionperformed (ActionEvent e) {


if (e.getsource () = = BUTTON_OK) {


bool = false;


This.setday (); If you click the OK button, call Setday () to redraw the button


} else if (E.getsource () = Button_today) {


bool = true;


Year.setselectedindex (10);


Month.setselectedindex (Now_month);


This.setday (); If you click Today's button, get today's date


}


}


public static void Main (string[] args) {


Calendertrain ct = new Calendertrain ();


}


}

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.