Java calendar Coding

Source: Internet
Author: User

// Jcalendar. java

Import java. awt. BorderLayout;

Import java. awt. Color;
Import java. awt. Dimension;
Import java. awt. GridLayout;
Import java. awt. SystemColor;
Import java. awt. event. ActionEvent;
Import java. awt. event. KeyEvent;
Import java. awt. event. MouseEvent;
Import java. util. Calendar;
Import java. util. GregorianCalendar;
Import java. util. Locale;
Import java. util. Date;
Import java. util. StringTokenizer;

Import javax. swing. BorderFactory;
Import javax. swing. JButton;
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JPanel;
Import javax. swing. JTextField;
Import javax. swing. JToggleButton;
Import javax. swing. SwingConstants;
Import javax. swing. UIManager;

/**
* <P> Title: Swing calendar </p>
* <P> Description: operation date </p>
* @ Author duxu2004
* @ Version 1.0.1
*/

Class JCalendar extends JPanel {
// Dynamically represents the year, month, and day
Private int year = 0;
Private int month = 0;
Private int day = 0;
// Main panel
Private JPanel Main = new JPanel ();
// Daily panel
Private JPanel jPanelDay = new JPanel ();
// Monthly panel
Private JPanel jPanelMonth = new JPanel ();
// Year input location
Private JTextField Year = new JTextField ();
// Input location of the month
Private JTextField Month = new JTextField ();
// Reduce the month
Private JButton MonthDown = new JButton ();
// Add a month
Private JButton MonthUp = new JButton ();

Private JPanel jPanelButton = new JPanel ();
// Reduce the year
Private JButton YearDown = new JButton ();
// Add year
Private JButton YearUp = new JButton ();
// Display the date position
Private JLabel Out = new JLabel ();
// China time zone. You can extend the time zone setting function from here.
Private Locale l = Locale. CHINESE;
// Main Calendar
Private GregorianCalendar cal = new GregorianCalendar (l );
// Weekly panel
Private JPanel weekPanel = new JPanel ();
// Day button group
Private JToggleButton [] days = new JToggleButton [42];
// Day panel
Private JPanel Days = new JPanel ();
// Mark
Private JLabel jLabel1 = new JLabel ();
Private JLabel jLabel2 = new JLabel ();
Private JLabel jLabel3 = new JLabel ();
Private JLabel jLabel4 = new JLabel ();
Private JLabel jLabel5 = new JLabel ();
Private JLabel jLabel6 = new JLabel ();
Private JLabel jLabel7 = new JLabel ();
// The number of days selected
Private JToggleButton cur = null;
// Array of the number of days of the month, used to obtain the number of days of the month
// 1 2 3 4 5 6 7 8 9 10 11 12
Private int [] mm = {, 30, 31 };

// Empty date Constructor
Public JCalendar (){
Try {
JbInit ();
}
Catch (Exception e ){
E. printStackTrace ();
}

}
// Constructor with Date settings
Public JCalendar (int year, int month, int day ){
Cal. set (year, month, day );
Try {
JbInit ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}
// Constructor with calendar Input
Public JCalendar (GregorianCalendar calendar ){
Cal = calendar;
Try {
JbInit ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}
// Constructor with Date input
Public JCalendar (Date date ){
Cal. setTime (date );
Try {
JbInit ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}
// Initialize the component
Private void jbInit () throws Exception {
// Initialize year, month, and day
IniCalender ();

This. setLayout (new BorderLayout ());
This. setBorder (BorderFactory. createRaisedBevelBorder ());
This. setMaximumSize (new Dimensions (200,200 ));
This. setMinimumSize (new Dimension (200,200 ));
This. setPreferredSize (new Dimension (200,200 ));

Main. setLayout (new BorderLayout ());
Main. setBackground (SystemColor.info );
Main. setBorder (null );

Out. setBackground (Color. lightGray );
Out. setHorizontalAlignment (SwingConstants. CENTER );
Out. setMaximumSize (new Dimension (100, 19 ));
Out. setMinimumSize (new Dimension (100, 19 ));
Out. setPreferredSize (new Dimension (100, 19 ));

JLabel1.setForeground (Color. red );
JLabel1.setHorizontalAlignment (SwingConstants. CENTER );
JLabel1.setHorizontalTextPosition (SwingConstants. CENTER );
JLabel1.setText ("day ");
JLabel2.setForeground (Color. blue );
JLabel2.setHorizontalAlignment (SwingConstants. CENTER );
JLabel2.setHorizontalTextPosition (SwingConstants. CENTER );
JLabel2.setText ("6 ");
JLabel3.setHorizontalAlignment (SwingConstants. CENTER );
JLabel3.setHorizontalTextPosition (SwingConstants. CENTER );
JLabel3.setText ("5 ");
JLabel4.setHorizontalAlignment (SwingConstants. CENTER );
JLabel4.setHorizontalTextPosition (SwingConstants. CENTER );
JLabel4.setText ("4 ");
JLabel5.setHorizontalAlignment (SwingConstants. CENTER );
JLabel5.setHorizontalTextPosition (SwingConstants. CENTER );
JLabel5.setText ("3 ");
JLabel6.setBorder (null );
JLabel6.setHorizontalAlignment (SwingConstants. CENTER );
JLabel6.setHorizontalTextPosition (SwingConstants. CENTER );
JLabel6.setText ("2 ");
JLabel7.setBackground (Color. lightGray );
JLabel7.setForeground (Color. black );
JLabel7.setBorder (null );
JLabel7.setHorizontalAlignment (SwingConstants. CENTER );
JLabel7.setHorizontalTextPosition (SwingConstants. CENTER );
JLabel7.setText ("1 ");

WeekPanel. setBackground (UIManager. getColor ("InternalFrame. activeTitleGradient "));
WeekPanel. setBorder (BorderFactory. createEtchedBorder ());
WeekPanel. setLayout (new GridLayout (1, 7 ));
WeekPanel. add (jLabel1, null );
WeekPanel. add (jLabel7, null );
WeekPanel. add (jLabel6, null );
WeekPanel. add (jLabel5, null );
WeekPanel. add (jLabel4, null );
WeekPanel. add (jLabel3, null );
WeekPanel. add (jLabel2, null );

MonthUp. setAlignmentX (float) 0.0 );
MonthUp. setActionMap (null );

JPanelMonth. setBackground (SystemColor.info );
JPanelMonth. setLayout (new BorderLayout ());
JPanelMonth. setBorder (BorderFactory. createEtchedBorder ());

Month. setBorder (null );
Month. setHorizontalAlignment (SwingConstants. CENTER );
Month. addMouseListener (new java. awt. event. MouseAdapter (){
Public void mouseClicked (MouseEvent e ){
Month_mouseClicked (e );
}
});
Month. addKeyListener (new java. awt. event. KeyAdapter (){
Public void keyPressed (KeyEvent e ){
Month_keyPressed (e );
}
});

MonthDown. setBorder (null );
MonthDown. setText ("\ u25C4 ");
MonthDown. addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Monthdown_actionreceivmed (e );
}
});
MonthUp. setBorder (null );
MonthUp. setText ("\ u25BA ");
MonthUp. addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Monthup_actionreceivmed (e );
}
});

JPanelButton. setLayout (null );
JPanelButton. setBorder (null );
JPanelButton. addComponentListener (new java. awt. event. ComponentAdapter (){
Public void componentResized (java. awt. event. ComponentEvent evt ){
JPanelButtonComponentResized (evt );
}
});

Year. setBorder (BorderFactory. createEtchedBorder ());
Year. setMaximumSize (new Dimension (80, 25 ));
Year. setMinimumSize (new Dimension (80, 25 ));
Year. setPreferredSize (new Dimension (80, 25 ));
Year. setHorizontalAlignment (SwingConstants. CENTER );
Year. addMouseListener (new java. awt. event. MouseAdapter (){
Public void mouseClicked (MouseEvent e ){
Year_mouseClicked (e );
}
});
Year. addKeyListener (new java. awt. event. KeyAdapter (){
Public void keyPressed (KeyEvent e ){
Year_keyPressed (e );
}
});

YearDown. setBorder (null );
YearDown. setMaximumSize (new Dimension (16, 16 ));
YearDown. setMinimumSize (new Dimension (16, 16 ));
YearDown. setPreferredSize (new Dimension (16, 16 ));
YearDown. setSize (new Dimension (16, 16 ));
YearDown. setText ("success ");
YearDown. addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Yeardown_actionreceivmed (e );
}
});
YearUp. setBorder (null );
YearUp. setMaximumSize (new Dimension (16, 16 ));
YearUp. setMinimumSize (new Dimension (16, 16 ));
YearUp. setPreferredSize (new Dimension (16, 16 ));
YearUp. setSize (new Dimension (16, 16 ));
YearUp. setText ("▲ ");
YearUp. addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Yearup_actionreceivmed (e );
}
});

JPanelDay. setLayout (new BorderLayout ());

Days. setLayout (new GridLayout (6, 7 ));
Days. setBackground (SystemColor.info );

For (int I = 0; I <42; I ++ ){
Days [I] = new JToggleButton ();
Days [I]. setBorder (null );
Days [I]. setBackground (SystemColor.info );
Days [I]. setHorizontalAlignment (SwingConstants. CENTER );
Days [I]. setHorizontalTextPosition (SwingConstants. CENTER );
// Days [I]. setSize (l, l );
Days [I]. addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Day = Integer. parseInt (JToggleButton) e. getSource (). getText ());
ShowDate ();
ShowDays ();
}
});
Days. add (days [I]);
}

This. add (Main, BorderLayout. NORTH );
This. add (jPanelDay, BorderLayout. CENTER );
This. add (jPanelMonth, BorderLayout. SOUTH );

Main. add (Year, BorderLayout. CENTER );
Main. add (Out, BorderLayout. WEST );
Main. add (jPanelButton, BorderLayout. EAST );

JPanelButton. add (YearUp );
JPanelButton. add (YearDown );

JPanelDay. add (weekPanel, BorderLayout. NORTH );
JPanelDay. add (Days, BorderLayout. CENTER );

JPanelMonth. add (Month, BorderLayout. CENTER );
JPanelMonth. add (MonthDown, BorderLayout. WEST );
JPanelMonth. add (MonthUp, BorderLayout. EAST );

ShowMonth ();
ShowYear ();
ShowDate ();
ShowDays ();
}

// Customize the year Selection Panel
Void jPanelButtonComponentResized (java. awt. event. ComponentEvent evt ){
YearUp. setLocation (0, 0 );
YearDown. setLocation (0, YearUp. getHeight ());
JPanelButton. setSize (YearUp. getWidth (), YearUp. getHeight () * 2 );
JPanelButton. setPreferredSize (new Dimension (YearUp. getWidth (), YearUp. getHeight () * 2 ));
JPanelButton. updateUI ();
}

// For testing
Public static void main (String [] args ){
JFrame f = new JFrame ();
F. setContentPane (new JCalendar ());
F. pack ();
// F. setResizable (false );
F. show ();
}

// Add year
Void yearup_actionreceivmed (ActionEvent e ){
Year ++;
ShowYear ();
ShowDate ();
ShowDays ();
}

// Reduce the year
Void yeardown_actionreceivmed (ActionEvent e ){
Year --;
ShowYear ();
ShowDate ();
ShowDays ();
}

// Reduce the month
Void monthdown_actionreceivmed (ActionEvent e ){
Month --;
If (month <0 ){
Month = 11;
Year --;
ShowYear ();
}
ShowMonth ();
ShowDate ();
ShowDays ();
}

// Add a month
Void monthup_actionreceivmed (ActionEvent e ){
Month ++;
If (month = 12 ){
Month = 0;
Year ++;
ShowYear ();
}
ShowMonth ();
ShowDate ();
ShowDays ();
}

// Initialize year, month, and day
Void iniCalender (){
Year = cal. get (Calendar. YEAR );
Month = cal. get (Calendar. MONTH );
Day = cal. get (Calendar. DAY_OF_MONTH );
}

// Refresh the month
Void showMonth (){
Month. setText (Integer. toString (month + 1) + "Month ");
}

// Refresh year
Void showYear (){
Year. setText (Integer. toString (year) + "Year ");
}

// Refresh date
Void showDate (){
Out. setText (Integer. toString (year) + "-" + Integer. toString (month + 1) + "-" + Integer. toString (day ));
}

// Select the number of days for re-painting.
Void showDays (){
Cal. set (year, month, 1 );
Int firstDayOfWeek = cal. get (Calendar. DAY_OF_WEEK );
Int n = mm [month];
If (cal. isLeapYear (year) & month = 1) n ++;
Int I = 0;
For (; I <firstDayOfWeek-1; I ++ ){
Days [I]. setEnabled (false );
Days [I]. setSelected (false );
Days [I]. setText ("");
}
Int d = 1;
For (; d <= n; d ++ ){
Days [I]. setText (Integer. toString (d ));
Days [I]. setEnabled (true );
If (d = day) days [I]. setSelected (true );
Else days [I]. setSelected (false );;
I ++;
}
For (; I <42; I ++ ){
Days [I]. setEnabled (false );
Days [I]. setSelected (false );
Days [I]. setText ("");
}
}

// Click the year panel to select the entire year string
Void SelectionYear (){
Year. setSelectionStart (0 );
Year. setSelectionEnd (Year. getText (). length ());
}

// Click the month panel to select the entire month string
Void SelectionMonth (){
Month. setSelectionStart (0 );
Month. setSelectionEnd (Month. getText (). length ());
}

// The month panel responds to the mouse-Click Event
Void Month_mouseClicked (MouseEvent e ){
// SelectionMonth ();
InputMonth ();
}

// Check the input month
Void inputMonth (){
String s;
If (Month. getText (). endsWith ("Month "))
{
S = Month. getText (). substring (0, Month. getText (). length ()-1 );
}
Else s = Month. getText ();
Month = Integer. parseInt (s)-1;
This. showMe ();
}

// Response to the keyboard hitting event on the month panel
Void Month_keyPressed (KeyEvent e ){
If (e. getKeyChar () = 10)
InputMonth ();
}

// The year panel responds to the mouse click event
Void Year_mouseClicked (MouseEvent e ){
// SelectionYear ();
InputYear ();
}

// Year keyboard strike Event Response
Void Year_keyPressed (KeyEvent e ){
// System. out. print (new Integer (e. getKeyChar (). byteValue ());
If (e. getKeyChar () = 10)
InputYear ();
}

// Check the input year string
Void inputYear (){
String s;
If (Year. getText (). endsWith ("Year "))
{
S = Year. getText (). substring (0, Year. getText (). length ()-1 );
}
Else s = Year. getText ();
Year = Integer. parseInt (s );
This. showMe ();
}

// Returns the date in string format, yyyy-mm-dd
Public String getDate () {return Out. getText ();}

// Enter the date in string format, yyyy-mm-dd
Public void setDate (String date ){
If (date! = Null ){
StringTokenizer f = new StringTokenizer (date ,"-");
If (f. hasMoreTokens ())
Year = Integer. parseInt (f. nextToken ());
If (f. hasMoreTokens ())
Month = Integer. parseInt (f. nextToken ());
If (f. hasMoreTokens ())
Day = Integer. parseInt (f. nextToken ());
Cal. set (year, month, day );
}
This. showMe ();
}

// Enter a date as a date object
Public void setTime (Date date ){
Cal. setTime (date );
This. iniCalender ();
This. showMe ();
}

// Returns the date object.
Public Date getTime () {return cal. getTime ();}

// Returns the current day
Public int getDay (){
Return day;
}

// Set the current day
Public void setDay (int day ){
This. day = day;
Cal. set (this. year, this. month, this. day );
This. showMe ();
}

// Set the current year
Public void setYear (int year ){
This. year = year;
Cal. set (this. year, this. month, this. day );
This. showMe ();
}

// Returns the current year.
Public int getYear (){
Return year;
}

// Returns the current month.
Public int getMonth (){
Return month;
}

// Set the current month
Public void setMonth (int month ){
This. month = month;
Cal. set (this. year, this. month, this. day );
This. showMe ();
}

// Refresh
Public void showMe (){
This. showDays ();
This. showMonth ();
This. showYear ();
This. showDate ();
}
}

Test code

JFrame f = new JFrame ();
F. setContentPane (new JCalendar ());
F. pack ();
// F. setResizable (false );
// F. show ();
F. setVisible (true );

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.