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

📄 mycalendarheader.java

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

public class MyCalendarHeader extends CustomItem {

	private final static int SELECT_YEAR = 0;

	private final static int SELECT_MONTH = 1;

	private final static int SELECT_NONE = 2;

	private int location = SELECT_NONE;

	private Calendar cal;
	private MyCalendar myCal;

	private int year, month, day;

	public MyCalendarHeader(String title, MyCalendar myCal) {
		super(title);		
		this.myCal = myCal;
		//      1月对应的数值是0,星期天对应数值是1 ,星期1是2,以此类推
		cal = myCal.getSelectedDate();
		year = cal.get(Calendar.YEAR);
		day = cal.get(Calendar.DAY_OF_MONTH);
		System.out.println(day);
		month = cal.get(Calendar.MONTH)+1;
		

	}

	protected int getMinContentHeight() {

		return 40;
	}

	protected int getMinContentWidth() {

		return 130;
	}

	protected int getPrefContentHeight(int width) {

		return 40;
	}

	protected int getPrefContentWidth(int height) {

		return 130;
	}

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

		int oldColor = g.getColor();	
		
		if(location == SELECT_YEAR){
			//			填充选定的日期的颜色
			g.setColor(0x00D0D0D0);
		    g.fillRect(30, 2, 60, 18);//int x, int y, int width, int height
			
		}else if(location == SELECT_MONTH){
			g.setColor(0x00D0D0D0);
			g.fillRect(35, 22, 50, 38);
		}
		g.setColor(oldColor);
		
		// store clipping properties
		int oldClipX = g.getClipX();
		int oldClipY = g.getClipY();
		int oldClipWidth = g.getClipWidth();
		int oldClipHeight = g.getClipHeight();
		
		//设置裁减区域的大小
		g.setClip(30, 2, 100, 18);		
		g.drawString("<<"+String.valueOf(year)+ "年>>", 31, 2, Graphics.LEFT | Graphics.TOP);
		
		g.setClip(35, 22, 100, 38);	
		g.drawString("<<"+String.valueOf(month)+"月 >>", 35, 22, Graphics.LEFT | Graphics.TOP);
			// 保存原来的画布信息
		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 == SELECT_YEAR){
				location = SELECT_MONTH;				
			}
			else if(location == SELECT_MONTH){
				location = SELECT_NONE;
				return false; //转移到下一个控件
			}else if(location == SELECT_NONE){
				location = SELECT_YEAR;
			}
			repaint();
			break;

		case Canvas.UP:
			if(location == SELECT_YEAR){
				location = SELECT_NONE;
				return false; //转移到下一个控件
			}else 	if(location == SELECT_MONTH){
				location = SELECT_YEAR;		
				
			}else 	if(location == SELECT_NONE){
				location = SELECT_MONTH;
				
			}
			repaint();
			
			break;

		case Canvas.LEFT:
			if(location == SELECT_YEAR){
				year--;
				repaint();
				myCal.setSelectedDate(year,month - 1,day);
			}
			if(location == SELECT_MONTH){
				if(month>1)
					month--;
				repaint();
				myCal.setSelectedDate(year,month - 1,day);
			}
			
			break;

		case Canvas.RIGHT:
			if(location == SELECT_YEAR){
				year++;
				repaint();
				myCal.setSelectedDate(year,month - 1,day);
			}
			if(location == SELECT_MONTH){
				month = month%12 + 1;
				repaint();
				myCal.setSelectedDate(year,month - 1,day);
			}
			break;
		

		}
		
		return true;
	}
	
}

⌨️ 快捷键说明

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