Java 编写日历程序

直接贴代码

package com.chent.dome.rili;

import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @author Administrator
 *
 */
public class CalendarBean {
	int year = 0, month = 0;

	public CalendarBean() {
		Calendar rili = Calendar.getInstance();
		rili.setTime(new Date());
		year = rili.get(Calendar.YEAR);
		month = rili.get(Calendar.MONTH) + 1;// Calendar.MONTH应该等于实际月份减一
	}

	public void setYear(int _year) {
		year = _year;
	}

	public void setMonth(int _month) {
		month = _month;
	}

	public String[] getCalendar() {
		String a[] = new String[42];
		Calendar c = Calendar.getInstance();
		c.set(year, month - 1, 1);
		int WeekDay = c.get(Calendar.DAY_OF_WEEK) - 1;// 计算出1号的星期
		int day = 0;
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
			day = 31;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			day = 30;
		}
		if (month == 2) {
			if ((year % 4 == 0) && (year % 100 != 0) || year % 400 == 0) {
				day = 29;
			} else {
				day = 28;
			}
		}
		for (int i = 0; i < WeekDay; i++) {
			a[i] = " ";
		}
		for (int i = WeekDay, n = 1; i < WeekDay + day; i++) {
			a[i] = String.valueOf(n);
			n++;
		}
		for (int i = WeekDay + day; i < a.length; i++) {
			a[i] = " ";
		}
		return a;
	}
}

package com.chent.dome.rili;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * 
 * @author Administrator
 *
 */
public class Calendar extends JFrame implements ActionListener  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JButton week[][], previousMonth, nextMonth;
	JLabel day[][], message;
	String w[] = { "日", "一", "二", "三", "四", "五", "六" };
	JPanel pCenter, pSouth;
	CalendarBean cb;
	String a[];
	public Calendar(String s) {
		super(s);
		cb = new CalendarBean();
		a = cb.getCalendar();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout(0, 0));
		JPanel pNorth = new JPanel();
		pNorth.setBackground(new Color(245, 222, 179));
		pNorth.setLayout(new FlowLayout());
		getContentPane().add(pNorth, BorderLayout.NORTH);
		previousMonth = new JButton("\u4E0A\u6708");
		previousMonth.setBackground(new Color(192, 192, 192));
		previousMonth.addActionListener(this);
		pNorth.add(previousMonth);
		nextMonth = new JButton("\u4E0B\u6708");
		nextMonth.setBackground(new Color(192, 192, 192));
		nextMonth.addActionListener(this);
	  	pNorth.add(nextMonth);
	  	pCenter = new JPanel();
	  	pCenter.setLayout(new GridLayout(7, 7));
	  	pCenter.setBackground(new Color(255, 250, 240));
	  	getContentPane().add(pCenter, BorderLayout.CENTER);
	  	pSouth = new JPanel();
	  	pSouth.setBackground(new Color(245, 222, 179));
	  	pSouth.setLayout(new FlowLayout());
	  	getContentPane().add(pSouth, BorderLayout.SOUTH);
	  	message = new JLabel("日历:" + cb.year + "年" + cb.month + "月");
	  	message.setFont(new Font("宋体", Font.BOLD, 15));
	  	pSouth.add(message);
	  	day=new JLabel[7][7];
	  	week=new JButton[7][7];
	  	for (int i = 0; i < 7; i++) {//周几用botton表示,日期用label表示
	  		for (int j = 0; j < 7; j++) {
	  			if (i == 0) {
	  				week[i][j] = new JButton(w[j]);
	  				week[i][j].setFont(new Font("宋体", Font.BOLD, 15));
	  				week[i][j].setBackground(new Color(192, 192, 192));
	  				pCenter.add(week[i][j]);
	  			} else {
	  				day[i][j] = new JLabel("");
	  				day[i][j].setFont(new Font("黑体", Font.BOLD, 15));
	  				day[i][j].setHorizontalAlignment(SwingConstants.CENTER);
	  				pCenter.add(day[i][j]);
	  			}
	  		}
	  	} 
	  	setBounds(100, 100, 450, 300);
	  	Toolkit kit = Toolkit.getDefaultToolkit();// 设置窗体居中显示
	  	Dimension screenSize = kit.getScreenSize();
	  	int screenWidth = screenSize.width / 2;
	  	int screenHeight = screenSize.height / 2;
	  	int windowWidth = getWidth();
	  	int windowHeight = getHeight();
	  	setLocation(screenWidth - windowWidth / 2, screenHeight - windowHeight / 2);
	  	setVisible(true);// 窗口的可见性设置
	  	PrintCalendar();
	  	validate();
	 }
	 public void PrintCalendar() {
		 int c = 0;
		 for (int i = 0; i < 7; i++) {
			 for (int j = 0; j < 7; j++) {
				 if (i != 0) {
					 day[i][j].setText(a[c++]);
				 }
			 }
		 }
		 message.setText("日历:" + cb.year + "年" + cb.month + "月");
	 }
	 @Override
	 public void actionPerformed(ActionEvent e) {
		 // TODO Auto-generated method stub
		 if (e.getSource() == nextMonth) {
			 int m = cb.month + 1;
			 if (m == 13) {
				 int y = cb.year + 1;
				 cb.setYear(y);
				 m = 1;
			 }
			 cb.setMonth(m);
			 a = cb.getCalendar();
			 PrintCalendar();
		 } else if (e.getSource() == previousMonth) {
			 int m = cb.month - 1;
			 if (m == 0) {
				 int y = cb.year - 1;
				 cb.setYear(y);
				 m = 12;
			 }
			 cb.setMonth(m);
			 a = cb.getCalendar();
			 PrintCalendar();
		 }
	 }
}

package com.chent.dome.rili;

/**
 * 
 * @author Administrator
 *
 */
public class MainClass {
	public static void main(String[] args) {
		new  Calendar("日历");//新建名为日历的窗口
	}
}

看一下效果:
在这里插入图片描述

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ublic class JCalendar extends JPanel{ //动态表示月日 private int year=0; private int month=0; private int day=0; private int first = 0; //用于标志日历面板是否显示 private static boolean isShow = false; //主面板 private JPanel Main = new JPanel(); //日面板 private JPanel jPanelDay = new JPanel(); //月面板 private JPanel jPanelMonth = new JPanel(); //月的输入位置 private JTextField Month = new JTextField(); //减少月份 private JButton MonthDown = new JButton(); //增月份 private JButton MonthUp = new JButton(); private JPanel jPanelButton = new JPanel(); //减少份 private JButton YearDown = new JButton(); //增份 private JButton YearUp = new JButton(); //显示日期的位置 private JLabel Out = new JLabel(); public JLabel Out2 = new JLabel(); private Locale l=Locale.CHINESE; //主日历 private GregorianCalendar cal=new GregorianCalendar(l); //星期面板 private JPanel weekPanel=new JPanel(); //天按钮组 private JToggleButton[] days=new JToggleButton[42]; //天面板 private JPanel Days = new JPanel(); //标示 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(); //当前选择的天数按钮 private JToggleButton cur=null; //月份天数数组,用来取得当月有多少天 // 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}; //空日期构造函数 public JCalendar() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //带日期设置的构造函数 public JCalendar(int year, int month, int day) { cal.set(year, month, day); try { jbInit(); } catch (Exception e) { e.printStackTrace(); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值