Java 如何从画布回到MIDlet

Java 如何从画布回到MIDlet,java,java-me,midp,lcdui,Java,Java Me,Midp,Lcdui,我试图弄清楚,在画布中的commandAction方法中,如何让我的命令按钮将我带回MIDlet的开始?(基本上重新开始) 当按键启动时,我会把它带到一个新的列表页面。在那个页面上,我有一个后退按钮,理想情况下,我需要它将我带回CalanderMidlet页面。我的逻辑是,在commandAction方法中,我只会从那里调用startApp(),但这不起作用,因为commandAction是静态的,startApp()不是 我已经用尽了所有的可能性,现在我被难倒了。任何指导都将不胜感激 在com

我试图弄清楚,在画布中的commandAction方法中,如何让我的命令按钮将我带回MIDlet的开始?(基本上重新开始)

当按键启动时,我会把它带到一个新的列表页面。在那个页面上,我有一个后退按钮,理想情况下,我需要它将我带回CalanderMidlet页面。我的逻辑是,在commandAction方法中,我只会从那里调用startApp(),但这不起作用,因为commandAction是静态的,startApp()不是

我已经用尽了所有的可能性,现在我被难倒了。任何指导都将不胜感激

在commandAction方法内的CalendarCanvas页面中,我注释了我被卡住的地方。我试图让后退按钮将我带回应用程序的开始(返回日历)

import java.io.IOException;
导入javax.microedition.midlet.*;
导入javax.microedition.lcdui.*;
/**
*@author addylesson
*/
公共类CalendarMidlet扩展了MIDlet{
私人显示器;
私人指挥部f、e、l;
公共日历MIDlet(){}
公开作废startApp(){
最终可显示d=新日历画布(此);
e=新命令(“后退”,Command.Back,0);
f=新命令(“退出”,Command.Exit,1);
l=新命令(“列表”,Command.SCREEN,3);
d、 addCommand(f);
d、 addCommand(e);
d、 addCommand(l);
//mList.addCommand(e);
d、 setCommandListener(新CommandListener()
{
公共无效命令操作(命令c,可显示)
{
如果(c==e)
mydisplay.setCurrent(d);
如果(c==l)
//mydisplay.setCurrent(mList);
//else如果(c==f)
通知销毁();
}
} );
/*mList.setCommandListener(新CommandListener()
{
公共无效命令操作(命令c,可显示)
{
如果(c==e)
mydisplay.setCurrent(d);
else如果(c==f)
通知销毁();
}
} );*/
mydisplay=Display.getDisplay(这个);
mydisplay.setCurrent(d);
}
公共无效pauseApp(){
}
公共应用程序(布尔无条件){
}
}
导入java.io.IOException;
导入java.util.Date;
导入javax.microedition.lcdui.*;
导入javax.microedition.midlet.midlet;
公共类CalendarCanvas扩展画布
{
CalendarWidget calendar=null;
MIDlet-MIDlet=null;
私人警报;
私人名单;
私人指挥部f;
私人显示器;
String[]stringElements={“Celeberity”、“Sports”、“Zodiac”};
Image[]imageElements={loadImage(“/planet.png”),
loadImage(“/car.png”)、loadImage(“/hotel.png”)};
公共日历画布(最终MIDlet m)
{
this.midlet=m;
日历=新日历小部件(新日期());
calendar.headerFont=Font.getFont(Font.FACE\u比例、Font.STYLE\u粗体、Font.SIZE\u大号);
calendar.weekdayFont=Font.getFont(Font.FACE\u比例、Font.STYLE\u粗体、Font.SIZE\u中等);
calendar.weekdayBgColor=0xccccff;
calendar.weekdayColor=0x0000ff;
calendar.headerColor=0xffffff;
警报=新警报(“侦听”,“在这一天”
+calendar.getSelectedDate().toString()+“发生的事情”,null,null);
alert.setTimeout(alert.FOREVER);
mList=新列表(“保留类型”,List.IMPLICIT,
字符串元素、图像元素);
f=新命令(“后退”,Command.Back,0);
mList.addCommand(f);
mList.setCommandListener(新CommandListener()
{
公共无效命令操作(命令c,可显示)
{
如果(c==f)
//我如何回到MIDLET的开始??
if(c==List.SELECT_命令){
int index=mList.getSelectedIndex();
Alert alert2=新警报(“您的选择”,
“您选择了“+mList.getString(index)+”,
null,AlertType.INFO);
mydisplay=Display.getDisplay(midlet);
mydisplay.setCurrent(警报2);
}
}
} );
calendar.initialize();
}
按下受保护的无效键(int键)
{
int keyCode=getGameAction(键);
字符串day=calendar.getSelectedDate().toString();
如果(键代码==火灾)
{
/*Display.getDisplay(midlet).setCurrent(
新警报(“选定日期”,
calendar.getSelectedDate().toString(),null,
警报类型(确认)
);*/
Display.getDisplay(midlet).setCurrent(mList);
}
其他的
{
日历。按键(按键代码);
重新油漆();
}
}
受保护的空心漆(图g)
{
g、 设置颜色(0xffffff);
g、 fillRect(0,0,getWidth(),getHeight());
日历.油漆(g);
}
私有映像加载映像(字符串名称)
{
图像=空;
试一试{
image=image.createImage(名称);
}
捕获(ioe异常ioe){
系统输出打印项次(ioe);
}
返回图像;
}
}
导入java.util.Calendar;
导入java.util.Date;
导入javax.microedition.lcdui.*;
公共类日历小部件
{
静态最终字符串[]月份标签=新字符串[]{
“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”
};
静态最终字符串[]工作日标签=新St
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author addylesson
 */
public class CalendarMidlet extends MIDlet {
    private Display mydisplay;
    private Command f,e,l;

    public CalendarMidlet(){}

    public void startApp() {
        final Displayable d = new CalendarCanvas(this);

        e = new Command("Back", Command.BACK, 0);
        f = new Command("Exit", Command.EXIT, 1);
        l = new Command("List", Command.SCREEN, 3);
        d.addCommand(f);
        d.addCommand(e);
        d.addCommand(l);
        //mList.addCommand(e);
        d.setCommandListener(new CommandListener()
        {
            public void commandAction(Command c, Displayable s)
            {
                if(c == e)
                    mydisplay.setCurrent(d);
                if(c == l)
                    //mydisplay.setCurrent(mList);
                //else if(c == f)
            notifyDestroyed();
            }
        } );
        /*mList.setCommandListener(new CommandListener()
        {
            public void commandAction(Command c, Displayable s)
            {
                if(c == e)
                    mydisplay.setCurrent(d);
                else if(c == f)
            notifyDestroyed();
            }
        } );*/
        mydisplay = Display.getDisplay(this);
        mydisplay.setCurrent(d);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

}

import java.io.IOException;
import java.util.Date;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class CalendarCanvas extends Canvas
{
    CalendarWidget calendar = null;
    MIDlet midlet = null;
        private Alert alert;
        private List mList;
        private Command f;
        private Display mydisplay;

        String[] stringElements = { "Celeberity", "Sports", "Zodiac" };
        Image[] imageElements = { loadImage("/airplane.png"),
                              loadImage("/car.png"), loadImage("/hotel.png") };

    public CalendarCanvas(final MIDlet m)
    {

                this.midlet = m;

        calendar = new CalendarWidget(new Date());

        calendar.headerFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
        calendar.weekdayFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        calendar.weekdayBgColor = 0xccccff;
        calendar.weekdayColor = 0x0000ff;
        calendar.headerColor = 0xffffff;

                alert = new Alert("Listen", "On this day "
                        +calendar.getSelectedDate().toString()+ "stuff happened", null, null);
                alert.setTimeout(Alert.FOREVER);
                mList = new List("Reservation type", List.IMPLICIT,
                stringElements, imageElements);
                f = new Command("Back", Command.BACK, 0);
                mList.addCommand(f);
                mList.setCommandListener(new CommandListener()
                      {
                      public void commandAction(Command c, Displayable s)
                        {
                            if (c == f)
                                //HOW DO I GET BACK TO THE START OF THE MIDLET??


                            if (c == List.SELECT_COMMAND) {
                         int index = mList.getSelectedIndex();
                         Alert alert2 = new Alert("Your selection",
                        "You chose " + mList.getString(index) + ".",
                        null, AlertType.INFO);
                        mydisplay = Display.getDisplay(midlet);
                        mydisplay.setCurrent(alert2);
        }

                        }
                      } );


        calendar.initialize();
    }


    protected void keyPressed(int key)
    {
        int keyCode = getGameAction(key);
        String day = calendar.getSelectedDate().toString();
        if(keyCode == FIRE)
        {
            /*Display.getDisplay(midlet).setCurrent(
                new Alert("Selected date", 
                                calendar.getSelectedDate().toString(), null, 
                                AlertType.CONFIRMATION)
            );*/
                        Display.getDisplay(midlet).setCurrent(mList);
        }
        else
        {
            calendar.keyPressed(keyCode);

            repaint();
        }
    }

    protected void paint(Graphics g)
    {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());

        calendar.paint(g);
    }
        private Image loadImage(String name) 
        {
        Image image = null;
        try {
        image = Image.createImage(name);
        }
        catch (IOException ioe) {
        System.out.println(ioe);
        }
        return image;
        }




}


import java.util.Calendar;
import java.util.Date;

import javax.microedition.lcdui.*;

public class CalendarWidget
{
    static final String[] MONTH_LABELS = new String[]{
        "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
    };
    static final String[] WEEKDAY_LABELS = new String[]{
        "M", "T", "W", "T", "F", "S", "S"
    };
    /* starting week day: 0 for monday, 6 for sunday */
    public int startWeekday = 0;

    /* elements padding */
    public int padding = 2;

    /* cells border properties */
    public int borderWidth = 4;
    public int borderColor = 0x000000;

    /* weekday labels properties */
    public Font weekdayFont = Font.getDefaultFont();
    public int weekdayBgColor = 0x0000ff;
    public int weekdayColor = 0xffffff;

    /* header (month-year label) properties */
    public Font headerFont = Font.getDefaultFont();
    public int headerBgColor = 0x0000ff;
    public int headerColor = 0xffffff;

    /* cells properties */
    public Font font = Font.getDefaultFont();
    public int foreColor = 0x000000;
    public int bgColor = 0x9999ff;
    public int selectedBgColor = 0xffff00;
    public int selectedForeColor = 0xff0000;

    /* internal properties */
    int width = 0;
    int height = 0;
    int headerHeight = 0;
    int weekHeight = 0;
    int cellWidth = 0;
    int cellHeight = 0;

    /* internal time properties */
    long currentTimestamp = 0;
    Calendar calendar = null;
    int weeks = 0;

public CalendarWidget(Date date)
{
    calendar = Calendar.getInstance();

    //we'll see these 2 methods later
    setDate(date);

    initialize();
}
public Date getSelectedDate()
{
    return calendar.getTime();
}
public void setDate(Date d)
{
    currentTimestamp = d.getTime();

    calendar.setTime(d);

    //weeks number can change, depending on week starting day and month total days
    this.weeks = (int)Math.ceil(((double)getStartWeekday() + getMonthDays()) / 7);
}
public void setDate(long timestamp)
{
    setDate(new Date(timestamp));
}
void initialize()
{
    //let's initialize calendar size
    this.cellWidth = font.stringWidth("MM") + 3 * padding;
    this.cellHeight = font.getHeight() + 15 * padding;

    this.headerHeight = headerFont.getHeight() + 2 * padding;
    this.weekHeight = weekdayFont.getHeight() + 2 * padding;

    this.width = 7 * (cellWidth + borderWidth) + borderWidth;
    initHeight();
}
void initHeight()
{
    this.height = 
        headerHeight + weekHeight + 
        this.weeks * (cellHeight + borderWidth) + borderWidth;
}
int getMonthDays()
{
    int month = calendar.get(Calendar.MONTH);

    switch(month)
    {
    case 3:
    case 5:
    case 8:
    case 10:
        return 30;
    case 1:
        return calendar.get(Calendar.YEAR) % 4 == 0 && calendar.get(Calendar.YEAR) % 100 != 0 ? 29 : 28;
    default:
        return 31;
    }
}
int getStartWeekday()
{
    //let's create a new calendar with same month and year, but with day 1
    Calendar c = Calendar.getInstance();

    c.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
    c.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
    c.set(Calendar.DAY_OF_MONTH, 1);

    //we must normalize DAY_OF_WEEK returned value
    return (c.get(Calendar.DAY_OF_WEEK) + 5) % 7;
}
public void keyPressed(int key)
{
    switch(key)
    {
    case Canvas.UP:
        go(-7);
        break;
    case Canvas.DOWN:
        go(7);
        break;
    case Canvas.RIGHT:
        go(1);
        break;
    case Canvas.LEFT:
        go(-1);
        break;
    }
}
void go(int delta)
{
    int prevMonth = calendar.get(Calendar.MONTH);

    setDate(currentTimestamp + 86400000 * delta);

    //we have to check if month has changed
    //if yes, we have to recalculate month height
    //since weeks number could be changed
    if(calendar.get(Calendar.MONTH) != prevMonth)
    {
        initHeight();
    }
}
public void paint(Graphics g)
{
    //painting background
    g.setColor(bgColor);
    g.fillRect(0, 0, width, height);

    //painting header (month-year label)
    g.setFont(headerFont);
    g.setColor(headerColor);
    g.drawString(MONTH_LABELS[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.YEAR), width / 2, padding, Graphics.TOP | Graphics.HCENTER);

    //painting week days labels
    g.translate(0, headerHeight);

    g.setColor(weekdayBgColor);
    g.fillRect(0, 0, width, weekHeight);

    g.setColor(weekdayColor);
    g.setFont(weekdayFont);

    for(int i = 0; i < 7; i++)
    {
        g.drawString(WEEKDAY_LABELS[(i + startWeekday) % 7],
            borderWidth + i * (cellWidth + borderWidth) + cellWidth / 2,
            padding,
            Graphics.TOP | Graphics.HCENTER
        );
    }

    //painting cells borders
    g.translate(0, weekHeight);

    g.setColor(borderColor);

    for(int i = 0; i <= weeks; i++)
    {
        g.fillRect(0, i * (cellHeight + borderWidth), width, borderWidth);
    }
    for(int i = 0; i <= 7; i++)
    {
        g.fillRect(i * (cellWidth + borderWidth), 0, borderWidth, height - headerHeight - weekHeight);
    }

    //painting days
    int days = getMonthDays();
    int dayIndex = (getStartWeekday() - this.startWeekday + 7) % 7;

    g.setColor(foreColor);

    int currentDay = calendar.get(Calendar.DAY_OF_MONTH);

    for(int i = 0; i < days; i++)
    {
        int weekday = (dayIndex + i) % 7;
        int row = (dayIndex + i) / 7;

        int x = borderWidth + weekday * (cellWidth + borderWidth) + cellWidth / 2;
        int y = borderWidth + row * (cellHeight + borderWidth) + padding;

        //if this is the current day, we'll use selected bg and fore colors
        if(i + 1 == currentDay)
        {
            g.setColor(selectedBgColor);
            g.fillRect(
                borderWidth + weekday * (cellWidth + borderWidth), 
                borderWidth + row * (cellHeight + borderWidth), 
                cellWidth, cellHeight);
            g.setColor(selectedForeColor);
        }

        g.drawString("" + (i + 1), x, y, Graphics.TOP | Graphics.HCENTER);

        //if this is the current day, we must restore standard fore color 
        if(i + 1 == currentDay)
        {
            g.setColor(foreColor);
        }
    }
    //let's traslate back!
    g.translate(0, - headerHeight - weekHeight);
}
}
    // ...
    public void startApp() {
        // extract stuff to start over into dedicated method,
        // keep the rest here
        e = new Command("Back", Command.BACK, 0);
        f = new Command("Exit", Command.EXIT, 1);
        l = new Command("List", Command.SCREEN, 3);
        mydisplay = Display.getDisplay(this);
        startOver();
    }

    public void startOver() {
        // encapsulate stuff to start over here
        final Displayable d = new CalendarCanvas(this);

        d.addCommand(f);
        d.addCommand(e);
        d.addCommand(l);
        d.setCommandListener(new CommandListener()
        {
            public void commandAction(Command c, Displayable s)
            {
                if(c == e)
                    mydisplay.setCurrent(d);
                if(c == l)
                    //mydisplay.setCurrent(mList);
                //else if(c == f)
            notifyDestroyed();
            }
        } );
        mydisplay.setCurrent(d);
    }
    // ...
    // ...
    CalendarMidlet midlet = null; // was declared as MIDlet before

    // ...
    public CalendarCanvas(final CalendarMIDlet m) // parameter was MIDlet
    {

                this.midlet = m;
                // ...
    }
 if (c == f)
     m.startOver(); // take me back to the CalendarMidlet page