The Java version of the calendar program

Source: Internet
Author: User

Mainframe.java

Package Com.huowolf.mycalender;import Java.awt.borderlayout;import Java.awt.color;import java.awt.GridLayout; Import Java.awt.toolkit;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.util.calendar;import Javax.swing.jcombobox;import Javax.swing.jframe;import Javax.swing.JLabel;import Javax.swing.jpanel;public class MainFrame extends jframe{private static final long serialversionuid = 90464999L; JPanel panel = new JPanel (new BorderLayout ()); JPanel Panel1 = new JPanel (); JPanel Panel2 = new JPanel (New GridLayout (7, 7)); JPanel panel3 = new JPanel (); jlabel[] labels = new jlabel[49]; JLabel Y_label = new JLabel ("year"); JLabel M_label = new JLabel ("month"); jcombobox<string> com1 = new jcombobox<string> (); jcombobox<string> COM2 = new jcombobox<string> (); int Re_year,re_mouth;int x_size,y_size;// Get a Calendar object for the current time calendars now = calendar.getinstance ();p ublic MainFrame () {super ("perpetual calendar") SetSize (300, 300);//Get screen width x_size = (int) toolkit.getdefaulttoolkit ().Getscreensize (). getwidth ();//Get screen height y_size = (int) toolkit.getdefaulttoolkit (). Getscreensize (). GetHeight ();        SetLocation ((x_size-300)/2, (Y_SIZE-350)/2); setdefaultcloseoperation (Jframe.exit_on_close);        Panel1.add (Y_label);        Panel1.add (COM1);        Panel1.add (M_label);                Panel1.add (COM2); for (int i = 0; i < i++) {Labels[i] = new JLabel ("", jlabel.center);//sets the displayed character to center Panel2.ad        D (Labels[i]);        } panel3.add (New Clock (this));        Panel.add (Panel1, Borderlayout.north);        Panel.add (Panel2, Borderlayout.center);        Panel.add (Panel3, Borderlayout.south);        Panel.setbackground (Color.White);        Panel1.setbackground (Color.White);        Panel2.setbackground (Color.White);                Panel3.setbackground (Color.White);        Init ();        Com1.addactionlistener (New Clockaction ());        Com2.addactionlistener (New Clockaction ());        Setcontentpane (panel);  SetVisible (TRUE);      Prohibit dragging setresizable (false);} Class Clockaction implements ActionListener {public void actionperformed (ActionEvent e) {int C                _year, C_month, C_week; C_year = Integer.parseint (Com1.getselecteditem (). toString ()); Gets the currently selected year C_month = Integer.parseint (Com2.getselecteditem (). toString ())-1; Get the current month and subtract 1, the month in the computer is 0-11 C_week = use (C_year, c_month); Call function use, get day of the week Resetday (C_week, C_year, C_month);        Call function Resetday}}public void init () {int year, month_num, First_day_num;                String log[] = {"Day", "one", "two", "three", "four", "five", "six"};        for (int i = 0; i < 7; i++) {Labels[i].settext (log[i]);        } for (int i = 0; i <; i = i + 7) {labels[i].setforeground (color.red);//Set Sunday date to red} for (int i = 6; i <; i = i + 7) {labels[i].setforeground (Color.green);//Set Saturday date to Green       } for (int i = 1; i < 10000; i++) {Com1.additem ("" + i);        } for (int i = 1; i < i++) {Com2.additem ("" + i); } month_num = (int) (Now.get (Calendar.month)); Gets the current time of the month of year = (int) (Now.get (calendar.year)); Gets the current time of year Com1.setselectedindex (YEAR-1); The Settings drop-down list is displayed as the current year com2.setselectedindex (Month_num);        The Settings drop-down list is displayed for the current month First_day_num = use (year, month_num);        Resetday (First_day_num, year, month_num);        } public int use (int reyear, int remonth) {int week_num; Now.set (Reyear, Remonth, 1);    Set the time for the first day of the year you want to query week_num = (int) (Now.get (Calendar.day_of_week));//Get the first day of the week return week_num; } public void Resetday (int week_log, int year_log, int month_log) {int month_day_score;//number of days to store the month int c        Ount = 1;                Month_day_score = 0;        Calendar cal = Calendar.getinstance (); /*cal.set (year_log+1900, Month_log + 1, 1); Cal.add (Calendar.month,-1);        Previous month */Cal.set (year_log+1900, Month_log, 1); Month_day_score = Cal.getactualmaximum (calendar.day_of_month);//Get the last day of one months for (int i = 7; i <; i++) {//initialization        Label (excluding the subscript value of the first seven labels) labels[i].settext ("");        } Month_day_score = Month_day_score + week_log;        for (int i = Week_log; i < Month_day_score; i++, count++) {Labels[i+6].settext (count + "");//plus subscript values for the first seven labels }}public static void Main (string[] args) {jframe.setdefaultlookandfeeldecorated (true);//provides its Window for the newly created JDialog Tips for decorating. New MainFrame ();}}

Clock.java

Package Com.huowolf.mycalender;import Java.awt.canvas;import Java.awt.color;import java.awt.font;import Java.awt.graphics;import Java.text.simpledateformat;import Java.util.calendar;public class Clock extends Canvas          Implements Runnable{private static final long serialversionuid = 82782l;private Thread t;private String time;        Public Clock (MainFrame MF) {setSize (280,40);        SetBackground (Color.White);                T=new Thread (this);                          Instantiate thread T.start ();    @Overridepublic void Run () {while (true) {try {thread.sleep (1000);   Hibernate 1 seconds} catch (Interruptedexception e) {System.out.println ("exception occurred");}    Repaint (100);//Redraw component}}public void paint (Graphics g) {font f=new font ("Arial", font.bold,16) within 100 milliseconds; SimpleDateFormat sdf=new SimpleDateFormat ("yyyy ' year ' MM ' DD ' Day ' HH:mm:ss");//Format time display type Calendar now=    Calendar.getinstance ();        Time=sdf.format (Now.gettime ());    Get the current date and time G.setfont (f);    G.setcolor (Color.orange); g.DrawString (Time,45,25);}} 

Very good program, according to the network summary, it is worth learning!

The Java version of the calendar program

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.