📄 calendardemo.java
字号:
//CalendarDemo.java file
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
import calendar.*;
public class CalendarDemo extends MIDlet
implements CommandListener,ImportDateFilter,DateSelectListener
{
private Command exitCommand, testCommand;
private TextBox tb;
private CalendarCanvas myCanvas;
private int [][]holiday={{1,1},{3,8},{5,1},{6,1},{10,1}};//保存每年的节日
public CalendarDemo()
{
exitCommand =new Command("Exit",Command.EXIT,9);
testCommand =new Command("Calendar",Command.SCREEN,1);
tb = new TextBox("demo","CalendarDemo MIDlet",40,0);
tb.addCommand(exitCommand);
tb.addCommand(testCommand);
tb.setCommandListener(this);
//创建日历对象
myCanvas =new CalendarCanvas(new Date());
myCanvas.setImportDateFilter(this);
myCanvas.setDateSelectListener(this);
//为了在日历界面中也能退出程序,添加了新的命令并替换了命令处理对象
myCanvas.addCommand(exitCommand);
myCanvas.setCommandListener(this);
}
protected void startApp( ) throws MIDletStateChangeException
{
Display.getDisplay(this).setCurrent(tb);
}
protected void pauseApp( )
{
}
protected void destroyApp( boolean p1 )
{
}
public void commandAction(Command c,Displayable d)
{//处理命令
if (c ==exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
else if (c ==testCommand)
{//切换到日历界面
Display.getDisplay(this).setCurrent(myCanvas);
}
else if(d == myCanvas)
{//对于来自CalendarCanvas 对象的命令发送给对象自己处理
myCanvas.commandAction(c,d);
}
}
//实现 ImportDateFilter 接口,判断日期类型
public int isImportantDate(Calendar date)
{
int dayOW = date.get(Calendar.DAY_OF_WEEK );
if(dayOW == Calendar.SATURDAY || dayOW == Calendar.SUNDAY)
{//周末作为假日
return HOLIDAY;
}
int dayOM = date.get(Calendar.DAY_OF_MONTH);
int month = date.get(Calendar.MONTH);
for(int i=0;i<holiday.length;i++)
{//判断是否法定节日
if(dayOM == holiday[i][1] && month+1== holiday[i][0])
return HOLIDAY;
}
if(dayOM == 2 && month ==0)
{//用于测试目的,1月2日设置为包含有重要事件的节日
return BOTH;
}
if((dayOM == 26 || dayOM==27) && month ==0)
{//用于测试目的,1月26日和1月27日设置为包含有重要事件的日期
return IMPORTANTDAY;
}
return NORMALDAY;
}
//实现 DateSelectListener 接口,处理日期被选择的事件
public void dateSelected(CalendarCanvas c, Calendar date)
{
Display.getDisplay(this).setCurrent(tb);//显示TextBox作为界面
String dateStr="date="+ date.get(Calendar.YEAR)+"/" ;
dateStr +=(date.get(Calendar.MONTH)+1) +"/" +date.get(Calendar.DAY_OF_MONTH);
tb.setString(dateStr);//显示被选择的日期
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -