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

📄 calendardemo_vector.java

📁 图书中的经典例子 好好看看 会有帮助的。java2me版本的
💻 JAVA
字号:
//CalendarDemo_vector.java file ,使用一维数组保存记录
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
import calendar.*;

public class CalendarDemo_vector extends MIDlet 
			implements CommandListener,ImportDateFilter,DateSelectListener
{
    private Command exitCommand= new Command("Exit",Command.EXIT,1);
    //用于日程列表Form上的菜单
    private Command backCmd= new Command("Back",Command.BACK,1);
    private Command editCmd =new Command("Edit",Command.SCREEN,1);
    private Command addCmd = new Command("Add",Command.SCREEN,1);
    private Command delCmd = new Command("Del",Command.SCREEN,2);
    //用于修改日程Form上的菜单
    private Command okCmd = new Command("OK",Command.OK,1);
    private Command cancelCmd = new Command("Cancel",Command.EXIT,1);
    
    private CalendarListForm listForm =null;
    private CalendarEditForm editForm =null;
    private CalendarCanvas myCanvas;
    private int [][]holiday={{1,1},{3,8},{5,1},{6,1},{10,1}};
    private Displayable lastDis=null;//保存最后显示的窗口对象
    private Vector itemArray = null;
    private Calendar dateSelected = null;
    public CalendarDemo_vector()
    {
        exitCommand =new Command("Exit",Command.EXIT,1);
        //创建日历对象
        myCanvas =new CalendarCanvas(new Date());
        myCanvas.setImportDateFilter(this);
        myCanvas.setDateSelectListener(this);
        //替换日历对象的命令处理对象
        myCanvas.addCommand(exitCommand);
        myCanvas.setCommandListener(this);
        lastDis = myCanvas;
		//创建日程列表Form对象  
        listForm = new CalendarListForm();
        listForm.addCommand(backCmd);
        listForm.addCommand(addCmd);
        listForm.addCommand(editCmd);
        listForm.addCommand(delCmd);
        listForm.setCommandListener(this);
        //初始化日程条目数组
        itemArray = new Vector();
    }
    protected void startApp(  ) throws MIDletStateChangeException
    {
        Display.getDisplay(this).setCurrent(lastDis);
    }

    protected void pauseApp(  )
    {//暂停时保存当前显示的对象
    	lastDis = Display.getDisplay(this).getCurrent();
    }

    protected void destroyApp( boolean p1 )
    {
    }

    public void commandAction(Command c,Displayable d)
    {//处理命令
        if (c ==exitCommand)
        {
            destroyApp(false);
            notifyDestroyed();
        }
        else if (d == myCanvas)
        {//对于来自CalendarCanvas 对象的命令发送给对象自己处理
        	myCanvas.commandAction(c, d);
        }
        else if (c ==backCmd)
        {//切换到日历界面
        	Display.getDisplay(this).setCurrent(myCanvas);
        }
        else if (c ==addCmd)
        {//增加新日程
			editForm = new CalendarEditForm(dateSelected.getTime());//创建日程修改Form对象
	        editForm.addCommand(okCmd);
	        editForm.addCommand(cancelCmd);
	        editForm.setCommandListener(this);
	        Display.getDisplay(this).setCurrent(editForm);
        }
        else if (c ==editCmd)
        {//修改现有日程
        	ScheduleItem item = listForm.getSelectedItem();
        	if( null != item)
        	{
				editForm = new CalendarEditForm(item);//创建日程修改Form对象
		        editForm.addCommand(okCmd);
		        editForm.addCommand(cancelCmd);
		        editForm.setCommandListener(this);
		        Display.getDisplay(this).setCurrent(editForm);
		    }
		    else
		    {//当前没有被选中的日程,显示错误信息
		    	Alert alert=new Alert("Empty","No Schedule is selected",null,AlertType.ERROR );
		    	Display.getDisplay(this).setCurrent(alert,listForm);
		    }
        }
        else if(c == delCmd)
        {
        	ScheduleItem item = listForm.delSelectItem();
        	if (item != null)
        	{
	        	itemArray.removeElement(item);
	        	item = null;
	        }
		    else
		    {//当前没有被选中的日程,显示错误信息
		    	Alert alert=new Alert("Empty","No Schedule is selected",null,AlertType.ERROR );
		    	Display.getDisplay(this).setCurrent(alert,listForm);
		    }
        }
        else if(c == okCmd)
        {
        	if(editForm.getType() == CalendarEditForm.ADD)
        	{//增加日程
        		ScheduleItem item = editForm.getScheduleItem();
        		itemArray.addElement(item);
		        Display.getDisplay(this).setCurrent(listForm);
		        listForm.add(item);
        	}
        	else
        	{//更改原有日程
        		ScheduleItem item = editForm.getScheduleItem();
        		Display.getDisplay(this).setCurrent(listForm);
        		listForm.update(item);
        	}
        	editForm = null;
        }
        else if(c == cancelCmd)
        {//从 CalendarEditForm 中取消增加或修改
        	Display.getDisplay(this).setCurrent(listForm);
        	editForm = null;
        }
    }
    //实现 ImportDateFilter 接口,判断日期类型
    public int isImportantDate(Calendar date)
    {
    	int dayType = NORMALDAY;
    	for(int i=0;i<itemArray.size();i++)
    	{
    		ScheduleItem item = (ScheduleItem)itemArray.elementAt(i);
    		if(item.isDateMatch(date))
    		{
    			dayType = IMPORTANTDAY;
    			break;
    		}
    	}
        int dayOW = date.get(Calendar.DAY_OF_WEEK );
        if(dayOW == Calendar.SATURDAY || dayOW == Calendar.SUNDAY)
        {//周末作为假日
            return HOLIDAY + dayType;
        }
        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 + dayType;
        }
        return dayType;
    }
    //实现 DateSelectListener 接口,处理日期被选择的事件
    public void dateSelected(CalendarCanvas c, Calendar date)
    {
        String dateStr="date="+ date.get(Calendar.YEAR)+"/" ;
        dateStr +=(date.get(Calendar.MONTH)+1) +"/" +date.get(Calendar.DAY_OF_MONTH);
        dateSelected = date;
        listForm.reset(itemArray,dateSelected);
        Display.getDisplay(this).setCurrent(listForm);
    }
}

⌨️ 快捷键说明

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