⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycalendar.java

📁 用java编写的日历,非常适合处学者参考
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import java.util.*;

public class MyCalendar extends CustomItem implements ItemCommandListener {

	private final static Command CMD_SELECT = new Command("Select",
			Command.ITEM, 1);

	private Display display;	

	private int rows = 6;

	private int cols = 7;

	private int dx = 20;

	private int dy = 20;

	private int currentX = 0;

	private int currentY = 0;

	private String[][] data = new String[rows][cols];//存储表格的日期数字

	//  记录每月天数
	private static int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
			31, 30, 31 };

	private int curyear, curmonth, curday;//存储当前本机上的年月日

	private Calendar cal;

	private final static int UPPER = 0;

	private final static int IN = 1;

	private final static int LOWER = 2;

	private int location = UPPER;

	public MyCalendar(String title, Display d) {
		super(title);
		display = d;
		setDefaultCommand(CMD_SELECT);
		setItemCommandListener(this);

		//1月对应的数值是0,星期天对应数值是1 ,星期1是2,以此类推
		cal = Calendar.getInstance();
		curyear = cal.get(Calendar.YEAR);
		curmonth = cal.get(Calendar.MONTH);
		curday = cal.get(Calendar.DAY_OF_MONTH);

		setData(curyear, curmonth, curday);

	}

	protected int getMinContentHeight() {

		return (rows * dy) + 1;
	}

	protected int getMinContentWidth() {

		return (cols * dx) + 1;
	}

	protected int getPrefContentHeight(int width) {

		return (rows * dy) + 1;
	}

	protected int getPrefContentWidth(int height) {

		return (cols * dx) + 1;
	}

	protected void paint(Graphics g, int w, int h) {

		for (int i = 0; i <= rows; i++) {
			g.drawLine(0, i * dy, cols * dx, i * dy);
		}

		for (int i = 0; i <= cols; i++) {
			g.drawLine(i * dx, 0, i * dx, rows * dy);
		}

		int oldColor = g.getColor();
		g.setColor(0x00D0D0D0);
		//填充第一行的颜色
		g.fillRect(1, 1, cols * dx - 1, dy - 1);
		//填充选定的日期的颜色
		g.fillRect((currentX * dx) + 1, (currentY * dy) + 1, dx - 1, dy - 1);
		g.setColor(oldColor);

		for (int i = 0; i < rows; i++) {

			for (int j = 0; j < cols; j++) {

				if (data[i][j] != null) {

					// store clipping properties
					int oldClipX = g.getClipX();
					int oldClipY = g.getClipY();
					int oldClipWidth = g.getClipWidth();
					int oldClipHeight = g.getClipHeight();
					//设置裁减区域的大小
					g.setClip((j * dx) + 1, i * dy, dx - 1, dy - 1);
					g.drawString(data[i][j], (j * dx) + 5, ((i + 1) * dy) - 3,
							Graphics.BOTTOM | Graphics.LEFT);

					// 保存原来的画布信息
					g.setClip(oldClipX, oldClipY, oldClipWidth, oldClipHeight);
				}
			}
		}
	}

	protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
			int[] visRect_inout) {

		switch (dir) {

		case Canvas.DOWN:

			if (location == UPPER) {
				location = IN;
			} else {

				if (currentY < (rows - 1) && data[currentY + 1][currentX] != "") {
					currentY++;
					repaint(currentX * dx, (currentY - 1) * dy, dx, dy);
					repaint(currentX * dx, currentY * dy, dx, dy);
				} else {
					location = LOWER;
					return false; //转移到下一个控件

				}
			}
			break;

		case Canvas.UP:
			if (location == UPPER) {
				location = IN;
			} else {

				if (currentY > 1 && data[currentY - 1][currentX] != "") {
					currentY--;
					repaint(currentX * dx, (currentY + 1) * dy, dx, dy);
					repaint(currentX * dx, currentY * dy, dx, dy);
				} else {
					location = UPPER;
					return false; //转移到上一个控件

				}
			}
			break;

		case Canvas.LEFT:

			if (currentX > 0 && data[currentY][currentX - 1] != "") {
				currentX--;
				repaint((currentX + 1) * dx, currentY * dy, dx, dy);
				repaint(currentX * dx, currentY * dy, dx, dy);
			}

			break;

		case Canvas.RIGHT:

			if (currentX < (cols - 1) && data[currentY][currentX + 1] != "") {
				currentX++;
				repaint((currentX - 1) * dx, currentY * dy, dx, dy);
				repaint(currentX * dx, currentY * dy, dx, dy);
			}
			break;		
			
		}

		visRect_inout[0] = currentX;
		visRect_inout[1] = currentY;
		visRect_inout[2] = dx;
		visRect_inout[3] = dy;

		return true;
	}

	public void setText(String text) {
		data[currentY][currentX] = text;
		repaint(currentY * dx, currentX * dy, dx, dy);
	}

	public void commandAction(Command c, Item i) {
		if (c == CMD_SELECT) {

		}

	}

	//得到某年某个月的天数
	protected int getDaysOfMonth(int year, int month) {
		if (month != 1)//如果不是2月,直接返回保存的当月天数
			return daysOfMonth[month];
		if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
			return 29;
		return daysOfMonth[month];
	}

	//设置表格存储的日期数字
	protected void setData(int year, int month, int day) {
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH,day);
		int daysofweek = cal.get(Calendar.DAY_OF_WEEK);
		
		cal.set(Calendar.DAY_OF_MONTH, 1);
		int number = 1;//记录天数的增加
		int daysofmonth = getDaysOfMonth(year, month);
		//获得本月第一天是星期几
		int whichDay = cal.get(Calendar.DAY_OF_WEEK) - 1;

		data[0][0] = "日";
		data[0][1] = "一";
		data[0][2] = "二";
		data[0][3] = "三";
		data[0][4] = "四";
		data[0][5] = "五";
		data[0][6] = "六";

		for (int i = 1; i < rows; i++) {

			for (int j = 0; j < cols; j++) {
				if (j < whichDay && i == 1) {
					data[i][j] = "";// String.valueOf(number)
					continue;
				}
				if (number <= daysofmonth) {
					data[i][j] = String.valueOf(number);
					number++;
					continue;
				}
				data[i][j] = "";

			}
		}

		currentX = daysofweek - 1;
		currentY = (day + whichDay) / 7 ;
		if((day + whichDay) % 7 != 0) //如果不能整除
			currentY++;
		

	}

	public Calendar getSelectedDate() {
		cal.set(Calendar.YEAR, curyear);
		cal.set(Calendar.MONTH, curmonth);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		//获得本月第一天是星期几
		int whichDay = cal.get(Calendar.DAY_OF_WEEK) - 1;

		int selectedDay = (currentY - 1) * 7 - whichDay + currentX + 1;
		System.out.println(selectedDay);

		cal.set(Calendar.DAY_OF_MONTH, selectedDay);
		return cal;
	}
	public void setSelectedDate(int year, int month, int day){		
		setData(year,month,day);
		repaint();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -