Use a simple Swing Calendar Bean to learn Calendar class (2) JCALENDAR source code

zhaozj2021-02-16  52

In the previous section, we discussed the basic profile of Calendar, and this section we will use the Java.util's own GregorianCalendar - a standard calendar that actually instantizes to create a GUI calendar called JCALENDAR. As shown below:

First put the JCalendar's source code, and analyze that we will do it next section. You can also learn about the approximate situation through the comments in the code.

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;

/ ** *

Title: Swing Calendar *

description: Operation Date * @Version 1.0.1 * /

Public Class Jcalendar Extends JPanel {// Dynamic Representation Private Int Year = 0; Private Int Month = 0; Private Int Day = 0; // Main Plate Private JPanel Main = New JPanel (); // Sunday PRIVATE JPANEL JPaNelday = new jPanel (); // Monte board private jPanel jPanelMonth = new jPanel (); // year input location private jtextfield year = new jtextfield (); // month input location private jtextfield Month = new jtextfield (); // Reduce month private jbutton monthdown = new jbutton (); // Increase the month private jbutton monthp = new jbutton ();

Private JPanel JPanelButton = new jPanel (); // Reduction Year Private JButton Yeadown = New JButton (); // Added Year Private JButton Yearup = New JButton (); // Display Date PRIVATE JLABEL OUT = New Jlabel (); // China time zone, you can extend from here, you can set the function of time zone. Private local LOCALE.CHINESE; // Main Calendar Private Gregoriancalendar Cal = New Gregoriancalendar (L); // Week Panel Private JPanel WeekPanel = New JPanel (); // 天 按 钮组 [] days = new jtogglebutton [42]; // Sky panel private jPanel days = new jPanel (); // label 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 (); // current selection Number button Private JTOGGLEBUTTON CUR = NULL; / / Month day number array, how many days // 1 2 3 4 5 6 7 8 9 10 11 12 Private int [] mm = {31, 28, 31, 30 , 31, 30, 31, 31, 30, 31, 30, 31}; // Empty Date Constructor PUBLIC JCALENDAR () {Try {jbinit ();} catch (exception e) { E.PrintStackTrace ();

} // Trical JCalendar (int year, int month, int day) {Cal.Set (YEAR, MONTH, DAY); Try {jbinit ();} catch (exception e) {E.PrintStackTrace }}} // Trical JCalendar (Gregoriancalendar; try {jbinit () {jbinit () {jbinit ();}}} // with date input Constructor PUBLIC JCALENDAR (DATE) {Cal.SetTime (Date); try {jbinit ();} catch (exception e) {E.PrintStackTrace ();}} // Initialization Component Private Void Jbinit () THROWS Exception {/ / Initialization Year, Month, Japanese INicalender (); this.setLayout (New BorderLayout ()); this.setBorder (BorderFactory.createraisedBevelBorder ()); this.setmaximumsize (New Dimension (200, 200)); this.setminimumsize Dimension (200, 200)); this.SetPreferredSize (New Dimension (200, 200)); main.setLayout (new borderLayout ()); main.setBackground (systemcolor.info); main.setBorder (null); out.setBackGround; Out.setBackground (Color.lightgray); out.setho rizontalAlignment (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 ( "six"); jLabel3.setHorizontalAlignment (SwingConstants.CENTER); jLabel3.setHorizontalTextPosition (SwingConstants.CENTER); jLabel3.setText ( "five");

jLabel4.setHorizontalAlignment (SwingConstants.CENTER); jLabel4.setHorizontalTextPosition (SwingConstants.CENTER); jLabel4.setText ( "four"); jLabel5.setHorizontalAlignment (SwingConstants.CENTER); jLabel5.setHorizontalTextPosition (SwingConstants.CENTER); jLabel5.setText ( " three "); jLabel6.setBorder (null); jLabel6.setHorizontalAlignment (SwingConstants.CENTER); jLabel6.setHorizontalTextPosition (SwingConstants.CENTER); jLabel6.setText (" two "); jLabel7.setBackground (Color.lightGray); jLabel7.setForeground (Color.black); jLabel7.setBorder (null); jLabel7.setHorizontalAlignment (SwingConstants.CENTER); jLabel7.setHorizontalTextPosition (SwingConstants.CENTER); jLabel7.setText ( "a"); 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, NUL l); 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 actionPerformed (ActionEvent e) {MonthDown_actionPerformed (e);}}); MonthUp.setBorder (null); MonthUp.setText ( "/ u25BA"); MonthUp. addActionListener (new java.awt.event.ActionListener () {public void actionPerformed (ActionEvent e) {MonthUp_actionPerformed (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.Setminim umSize (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 ("▼");

YearDown.addActionListener (new java.awt.event.ActionListener () {public void actionPerformed (ActionEvent e) {YearDown_actionPerformed (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 actionPerformed (ActionEvent e) {YearUp_actionPerformed (e);}}); jPanelDay.setLayout (new BorderLayout ()); Days.setLayout (new GridLayout (6 , 7); days.setbackground; 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 actionPerformed (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 (Yeadown) 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 annual selection panel void JPanelButtonComponentresized (java.awt.Event.ComponEnteVent EVT) {Yearup.setLocation (0); Yeadown.setLocation (0, Yearup.getHeight (); jPanelButton.setsize (Yearup. getWidth (), Yearup.getHeight () * 2); jPanelButton.SetPreferRedsize (New Dimension (Yearup.GetWidth (), Yearup.getHEight () * 2)); jPanelButton.Updateui ();} // Test public static void Main (String [] args) {jframe f = new jframe (); f.setContentPane (new jcalendar ()); f.pack (); //f.setresizable (FALSE); f.show ();}

// increase year void YearUp_actionPerformed (ActionEvent e) {year ; showYear (); showDate (); showDays ();} // decrease year void YearDown_actionPerformed (ActionEvent e) {year--; showYear (); showDate (); showDays ();} // Reduce the month void monthdown_actionperformed (ActionEvent E) {MONTH -; IF (Month <0) {MONTH = 11; Year ---; showyear (); showMonth (); showdate (); showdays () } // increase the month void monthup_actionperformed (ActionEvent E) {MONTH ; if (Month == 12) {Month = 0; Year ; showyear ();} showMonth (); showdate (); showdays ();} // initialization Annual Month, Void INICAL () {Year = Cal.get (Calendar.Year); Month = Cal.Get; day = Cal.Get (Calendar.day_of_month);} // Brush the New Moon Void ShowMont () {MONTH.SETTEXT (Integer.Tostring (Month 1) "Month");} // Refreshment 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));} // Heavy painting days Select panel void showDays () {Cal.set (Year, Month, 1); int firstdayofwek = CAL.GET (Calendar.day_of_week); int N = mm [MONTH]; if (Cal.isleApyear (Year) && Month == 1) n ; int i = 0; for (; i

} For (; i <42; i ) {days [i] .setenabled (false); days [i] .setSelected (false); days [i] .Settext (");}} // Click the Year Panel Choose the entire year string void selectyear () {Year.SetSelectionStart (0); Year.SetSelectionend (Year.getText (). Length ());} // Click the Monster panel Select the entire month string void SelectionMont () { Month.setSelectionStart (0); Month.GetSelectionend (Month.getText (). Length ());} // Month panel Response Mouse Click Event Void Month_MouseClicked (MouseEvent E) {// SelectionMonth (); infutmonth ();} // Test the month void infutmonth () {string s; if (Month.getText (). Endswith) {s = month.getText (). Substring (0, Month.getText (). Length ) -1);} else s = month.getText (); month = integer.parseint (s) -1; this.showme ();} // month panel keyboard tap event response void month_keypressed (keyevent e) {i (E.GetKeychar () == 10) InputMonth ();} // Year panel response mouse Click Event Void Year_MouseClicked (MouseEvent E) {// selectionyear (); inputyear ();} // Year keyboard tap event response Void Year_KeyPressed (KeyEvent E) {//system.out.print(ne W Integer (E.GetKeychar ()). Bytevalue ()); if (E.GetKeyChar () == 10) InputYear ();} // Input Years 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 ();} // return date in string, YYYY-mm-dd public string getdate () {return out.gettext ();} // Enter the date in string 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.haasmoretoKens ()) DAY = Integer.Parseint (f.NextToken ()); Cal.Set (Year, Month, day);} this.showme (); } // Enter the date Public Void SetTime (Date Date); this.chhowme (); this.Showme (); this.Showme ();} // Return date object public Date gettime () {Return CAL .gettime ();} // Return the current day public int getday () {return day;} // set the current day public void setday (int day) {this.day = day; ca.set (this.Year, THIS.MONTH, THIS.DAY; this.showme ();} // Sets the current annual public void setYear (int year) {this.year = year; Cal.Set (this.year, this.month, this. Day); this.showme ();} // Returns the current annual public int gameEear () {returnies;} // Return the current month public int getMonth () {return month;} // Set the current month Public Void SetMonth {this.month = month; Cal.Set (this.year, this.month, this.day); this.showme ();} // refresh public void showme () {this.showdays () Im.Showmonth (); this.showyear (); this.showdate ();}}

转载请注明原文地址:https://www.9cbs.com/read-22268.html

New Post(0)