?? calendarcanvas.java
字號:
import java.util.*;
import javax.microedition.lcdui.*;
public class CalendarCanvas extends Canvas implements CommandListener
{
public static Command nextMonthCmd = new Command("NextMonth",Command.SCREEN,1);
public static Command prevMonthCmd = new Command("PrevMonth",Command.SCREEN,1);
public static Command nextYearCmd = new Command("nextYear",Command.SCREEN,1);
public static Command prevYearCmd = new Command("prevYear",Command.SCREEN,1);
public static Command selectCmd = new Command("select",Command.EXIT,2);
//周日到周六的文字
private static String weekDays = new String("SunMonTueWenThuFriSat");
private static Font[] fontShow={Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL),
Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL),
Font.getFont(Font.FACE_SYSTEM,Font.STYLE_UNDERLINED,Font.SIZE_SMALL),
Font.getFont(Font.FACE_SYSTEM,Font.STYLE_UNDERLINED|Font.STYLE_BOLD,Font.SIZE_SMALL)};
private static int[] clrBK= {0x00ffffff,0x00999999,0x00ffffff,0x00999999};
private static int[] clrFont={0x00000000,0x00000000,0x00000000,0x00000000};
private static Font fontSelected=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_SMALL);
private static int clrSelectedBK=0x00cccccc,clrSelectedFont = 0x00000000;
//記錄每月天數
private static int[] daysOfMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
//日期選擇事件處理對象
protected DateSelectListener dateListener = null;
//重要日期判斷對象
protected ImportDateFilter dateFilter = null;
//月份變動事件處理對象
protected MonthChangeListener monthListener =null;
private int width,height;
private int startX;//保存繪圖開始的X坐標位置,便于日歷居中顯示
private int iCellWidth,iCellHeight;//日歷顯示參數,每個日期所占空間
private Calendar calTemp;//日期對象
//當前焦點所在日期
private int curYear,curMonth,curDay;
public CalendarCanvas(Date date)
{
super();
addCommand(selectCmd);
addCommand(nextMonthCmd);
addCommand(prevMonthCmd);
addCommand(nextYearCmd);
addCommand(prevYearCmd);
setCommandListener(this);
calTemp = Calendar.getInstance();
if(date !=null)
calTemp.setTime(date);
curYear = calTemp.get(Calendar.YEAR);
curMonth = calTemp.get(Calendar.MONTH);
curDay = calTemp.get(Calendar.DAY_OF_MONTH);
iCellWidth = fontSelected.substringWidth(weekDays,0,3)+2;
iCellHeight=fontSelected.getHeight()+2;
}
public void setImportDateFilter(ImportDateFilter filter)
{
dateFilter = filter;
}
public void setDateSelectListener(DateSelectListener listener)
{
dateListener = listener;
}
public void setMonthChangeListener(MonthChangeListener listener)
{
monthListener = listener;
}
public Calendar getFocusDate()
{
calTemp.set(Calendar.YEAR,curYear);
calTemp.set(Calendar.MONTH,curMonth);
calTemp.set(Calendar.DAY_OF_MONTH,1);
return calTemp;
}
//在畫布顯示前保存屏幕尺寸
protected void showNotify()
{
height = getHeight();
width = getWidth();
startX = (width- 7*iCellWidth)/2;
}
protected void paint(Graphics g)
{
//填充屏幕
g.setColor(0x00ffffff);
g.fillRect(0,0,width,height);
paintMonth(g);
}
protected void keyPressed(int keyCode)
{
int newDay = curDay;
switch(getGameAction(keyCode))
{
case UP:
if(curDay>7)
newDay-=7;
break;
case DOWN:
if(curDay<=getDaysOfMonth(curYear,curMonth)-7)
newDay+=7;
break;
case LEFT:
if(curDay>1)
newDay --;
break;
case RIGHT:
if(curDay<getDaysOfMonth(curYear,curMonth))
newDay ++;
break;
case FIRE://確定選擇某個日期
selectDate();
break;
}
if(newDay !=curDay)
{
curDay = newDay;
repaint();
}
}
//處理菜單命令事件
public void commandAction(Command c,Displayable d)
{
if(c== nextYearCmd)//切換到下一年
{
curYear ++;
curMonth = 0;
notifyMonthChange();
}
else if(c==prevYearCmd)
{
curYear --;
curMonth =11;
notifyMonthChange();
}
else if(c== nextMonthCmd)
nextMonth();
else if(c== prevMonthCmd)
prevMonth();
else if(c== selectCmd)
{
selectDate();
}
repaint();
}
//繪制當月的日歷
protected void paintMonth(Graphics g)
{
//顯示當前年月日
int offsetY = 0; //保存繪圖的Y軸坐標
g.setColor(clrSelectedFont);
g.setFont(fontSelected);
g.drawString(curYear+"/"+(curMonth+1),startX+iCellWidth*7/2,offsetY+2,Graphics.HCENTER|Graphics.TOP);
//顯示一周中每天的時間
g.setColor(clrSelectedFont);
g.setFont(fontSelected);
offsetY+=iCellHeight;
for(int i=0;i<7;i++)
g.drawSubstring(weekDays,i*3,3,startX+i*iCellWidth+1,offsetY,Graphics.LEFT|Graphics.TOP);
//顯示當月所有日期
calTemp.set(Calendar.YEAR,curYear);
calTemp.set(Calendar.MONTH,curMonth);
int dayOW = 0;//保存星期
int clr1,clr2;//保存顏色值
Font ft;//保存字體
int type = ImportDateFilter.NORMALDAY;//日期的類型
int daysNum = getDaysOfMonth(curYear,curMonth);//得到當月的天數
offsetY+=iCellHeight;
for(int i=1;i<=daysNum;i++)
{
//分別對每天進行繪制
calTemp.set(Calendar.DAY_OF_MONTH,i);
dayOW = calTemp.get(Calendar.DAY_OF_WEEK);//得到當天是星期幾
if(i==curDay)
{
//當天是被選中的日期,用固定的字體和顏色進行顯示
ft = fontSelected;
clr1 = clrSelectedBK;
clr2 = clrSelectedFont;
}
else
{
//對于未選中的日期類型決定顯示方式
if(dateFilter !=null)
{
//判斷當天是否重要日期
type = dateFilter.isImportantDate(calTemp);
}
//type = i%4;
ft = fontShow[type];
clr1 = clrBK[type];
clr2 = clrFont[type];
}
g.setFont(ft);
g.setColor(clr1);
g.fillRect(startX+(dayOW-1)*iCellWidth,offsetY,iCellWidth-1,iCellHeight-1);
g.setColor(clr2);
g.drawString(String.valueOf(i),startX+(dayOW-1)*iCellWidth+iCellWidth/2,offsetY+1,Graphics.HCENTER|Graphics.TOP);
if(dayOW == Calendar.SATURDAY)
offsetY+=iCellHeight;//轉入下一周前增加Y軸坐標
}
}
//將月份改為下個月
protected void nextMonth()
{
if(curMonth<11)
curMonth ++;
else
{
curMonth = 0;
curYear ++;
}
curDay = 1;
notifyMonthChange();
}
protected void prevMonth()
{
if(curMonth>0)
curMonth--;
else
{
curMonth = 11;
curYear --;
}
curDay = 1;
notifyMonthChange();
}
//確定選擇某個日期并引發事件
protected void selectDate()
{
if(dateListener!=null)
{
//引發事件
calTemp.set(Calendar.YEAR,curYear);
calTemp.set(Calendar.MONTH,curMonth);
calTemp.set(Calendar.DAY_OF_MONTH,curDay);
dateListener.dateSelected(this,calTemp);
}
}
//月份變化通知
protected void notifyMonthChange()
{
if(monthListener !=null)
{
calTemp.set(Calendar.YEAR,curYear);
calTemp.set(Calendar.MONTH,curMonth);
calTemp.set(Calendar.DAY_OF_MONTH,curDay);
monthListener.monthChanged(this,calTemp);
}
}
//得到某年某月的天數
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];
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -