码迷,mamicode.com
首页 > 编程语言 > 详细

java版的日历程序

时间:2014-11-20 18:54:06      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:java   日历   canvas   线程   calendar   

//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("年份");
	JLabel m_label = new JLabel("月份");
	JComboBox<String> com1 = new JComboBox<String>();
	JComboBox<String> com2 = new JComboBox<String>();
	
	int re_year,re_mouth;
	int x_size,y_size;
	//得到一个当前时间的日历对象
	Calendar now = Calendar.getInstance();
	
	public MainFrame() {
		super("万年历");
		setSize(300, 300);
		//获取屏幕宽度
		x_size = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		//获取屏幕高度
		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 < 49; i++) {
            labels[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中
            panel2.add(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);
        //禁止拖动
        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()); // 得到当前所选年份
                c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
                c_week = use(c_year, c_month); // 调用函数use,得到星期几
                
                Resetday(c_week, c_year, c_month); // 调用函数Resetday
            }
        }
	
	public void init() {
        int year, month_num, first_day_num;
        String log[] = { "日", "一", "二", "三", "四", "五", "六" };
        
        for (int i = 0; i < 7; i++) {
            labels[i].setText(log[i]);
        }
        for (int i = 0; i < 49; i = i + 7) {
            labels[i].setForeground(Color.red); // 将星期日的日期设置为红色
        }
        for (int i = 6; i < 49; i = i + 7) {
            labels[i].setForeground(Color.green);// 将星期六的日期设置为绿色
        }
        for (int i = 1; i < 10000; i++) {
            com1.addItem("" + i);
        }
        for (int i = 1; i < 13; i++) {
            com2.addItem("" + i);
        }
        
        
        
        month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份
        year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份
        com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年
        com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月
        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); // 设置时间为所要查询的年月的第一天
	        week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
	        return week_num;
	    }
	  
	  public void Resetday(int week_log, int year_log, int month_log) {
	        int month_day_score; // 存储月份的天数
	        int count =1;
	        month_day_score = 0;
	        
	        Calendar cal = Calendar.getInstance();
	        /*cal.set(year_log+1900, month_log + 1, 1);
	        cal.add(Calendar.MONTH, -1); // 前个月
	        */	 
	        cal.set(year_log+1900, month_log, 1);
	        month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);//得到一个月中最后一天

	        for (int i = 7; i < 49; i++) { // 初始化标签(除去前七个标签的下标值)
	            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 + "");//加上前七个标签的下标值
	        }
	    }

	public static void main(String[] args) {
		 JFrame.setDefaultLookAndFeelDecorated(true);//为新创建的 JDialog提供其 Window 装饰的提示。
		 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);                //实例化线程
    	    t.start();                        			
	  }

	@Override
	public void run() {
		while(true) {
				try {
				Thread.sleep(1000);    			//休眠1秒钟
			} catch (InterruptedException e) {
				System.out.println("发生异常");
			}   	
			repaint(100);//在100毫秒内重绘组件
		}
	}
	
	
		public void paint(Graphics g) {
			Font f=new Font("宋体",Font.BOLD,16);
		    SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型
		    Calendar now=Calendar.getInstance();
		    time=SDF.format(now.getTime());        //得到当前日期和时间
		    g.setFont(f);
		    g.setColor(Color.orange);
		    g.drawString(time,45,25);
	
		}
}

//很不错的程序,根据网络总结的,值得学习!

java版的日历程序

标签:java   日历   canvas   线程   calendar   

原文地址:http://blog.csdn.net/huolang_vip/article/details/41316119

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!