2

我已经问过了,但我需要进一步的知识。我正在尝试使用按钮命令将两个类链接在一起。我在这里遵循了最后一个答案,但是当我分开课程时,它不起作用。我使用 b2Command 来触发事件。这是我的代码:

MainMidlet.java

  /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
  */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Display;

/**
* @author bon
*/
public class MainMidlet extends MIDlet implements CommandListener, 
ItemCommandListener, ItemStateListener{
private Display display;
private Form form;
private Command okCommand, backCommand, exitCommand, b1Command, b2Command, exCommand,    boutCommand;
private StringItem b1stringItem, b2stringItem, boutstringItem, exstringItem;

public AddCalendarEvent addcalendarevent;

public MainMidlet(){
    okCommand = new Command ( "OK", Command.OK, 1); 
    exitCommand = new Command ( "EXIT", Command.EXIT, 1);
    backCommand = new Command ( "BACK", Command.BACK, 1);
    b1Command = new Command ( "BUTTON", Command.ITEM, 2);
    b2Command = new Command ( "BUTTON", Command.ITEM, 2);
    exCommand = new Command ( "BUTTON", Command.ITEM, 2);
    boutCommand = new Command ( "BUTTON", Command.ITEM, 2);

    form = new Form ( "CyberMe");

    b1stringItem = new StringItem (null, "Make Todo", Item.BUTTON); 
    b1stringItem.setItemCommandListener (this); 
    b1stringItem.setDefaultCommand (b1Command);

    b2stringItem = new StringItem (null, "Set Alarm", Item.BUTTON); 
    b2stringItem.setItemCommandListener (this); 
    b2stringItem.setDefaultCommand (b2Command);

    boutstringItem = new StringItem (null, "About", Item.BUTTON); 
    boutstringItem.setItemCommandListener (this); 
    boutstringItem.setDefaultCommand (boutCommand);

    exstringItem = new StringItem (null, "Exit", Item.BUTTON); 
    exstringItem.setItemCommandListener (this); 
    exstringItem.setDefaultCommand (exCommand);

    form.append ( "Any Appointment?"); 
    form.append (b1stringItem);
    form.append (b2stringItem);
    form.append (boutstringItem);
    form.append (exstringItem);

    form.addCommand (okCommand); 
    form.addCommand (exitCommand); 
    form.setCommandListener (this);
}

public void startApp() {
    display = Display.getDisplay(this);
    display.setCurrent(form);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
    throw new UnsupportedOperationException("Not supported yet.");
}

public void commandAction(Command c, Item item) {

if (c == b1Command) { 
//display.setCurrent (); 
}
else if (c == b2Command){
    addcalendarevent = new AddCalendarEvent(this);
    display.setCurrent(addcalendarevent);
}
else if (c == exCommand){
destroyApp(false); 
notifyDestroyed ();
}
//else if (c == boutCommand)
//display.setCurrent ();
}

public void itemStateChanged(Item item) {
    throw new UnsupportedOperationException("Not supported yet.");
}
}

这就是我要链接的内容:AddCalendarEvent.java

        /*
        * To change this template, choose Tools | Templates
        * and open the template in the editor.
        */
        import javax.microedition.midlet.MIDlet;
        import javax.microedition.lcdui.Displayable;
        import javax.microedition.lcdui.Display;
        import javax.microedition.lcdui.Form;
        import javax.microedition.lcdui.CommandListener;
        import javax.microedition.lcdui.TextField;
        import javax.microedition.lcdui.DateField;
        import javax.microedition.lcdui.Command;
        import javax.microedition.lcdui.Alert;

        import javax.microedition.pim.PIM;
        import javax.microedition.pim.PIMItem;
        import javax.microedition.pim.EventList;
        import javax.microedition.pim.Event;
        import javax.microedition.pim.PIMException;

        import java.util.Date;
        /**
        *
        * @author bon
        */
        public class AddCalendarEvent extends MainMidlet{
            private Display display;
            // Form where user can enter data of new event.
            private Form addEventForm;

            // Command for adding event to list of events. Placed on addEventForm.
            private Command cmdAddEvent;
            // Command for exiting from application. Placed on addEventForm.
            private Command cmdExit;

            // Text field for summary of event.
            private TextField summaryField;
            // Date field for start data of event.
            private DateField startDateField;
            // Date field for end data of event.
            private DateField endDateField;
            // Text field for note of event.
            private TextField noteField;
            // Text field for location of event.
            private TextField locationField;

            /**
            * Constructor.
            */    
            public AddCalendarEvent() {
                if(checkPIMSupport() == false) {
                    exitMIDlet();
                }

                initializeComponents();
            }

            /**
            * Initializes components of midlet.
            */      
            public void initializeComponents() {
                display = Display.getDisplay(this);

                // Create form for adding event.
                addEventForm = new Form("Add event");

                // Add commands to form and set listener for it.
                cmdAddEvent = new Command("Add event", Command.SCREEN, 0);
                addEventForm.addCommand(cmdAddEvent);

                cmdExit = new Command("Exit", Command.EXIT, 0);
                addEventForm.addCommand(cmdExit);

                addEventForm.setCommandListener(this);        

                try {
                    // Get list of events.
                    EventList eventList = (EventList)PIM.getInstance().openPIMList(
                            PIM.EVENT_LIST, PIM.READ_WRITE);

                    // Create controls based on supported fields for event.
                    if(eventList.isSupportedField(Event.SUMMARY) == true) {
                        summaryField = new TextField("Summary", null, 20, 
                                                    TextField.ANY);
                        addEventForm.append(summaryField);
                    } else {
                        // At least "summary" field must be supported. 
                        // If not, throw exception.
                        eventList.close();
                        throw new Exception("Summary field is not supported");
                    }

                    if(eventList.isSupportedField(Event.START) == true) {
                        startDateField = new DateField("Start date", 
                                                    DateField.DATE_TIME);
                        startDateField.setDate(new Date());
                        addEventForm.append(startDateField);
                    }

                    if(eventList.isSupportedField(Event.END) == true) {
                        endDateField = new DateField("End date", DateField.DATE_TIME);
                        endDateField.setDate(new Date());
                        addEventForm.append(endDateField);
                    }

                    if(eventList.isSupportedField(Event.NOTE) == true) {
                        noteField = new TextField("Note", null, 20, TextField.ANY);
                        addEventForm.append(noteField);
                    }

                    if(eventList.isSupportedField(Event.LOCATION) == true) {
                        locationField = new TextField("Location", null, 20, 
                                                    TextField.ANY);
                        addEventForm.append(locationField);
                    }            

                    // Close list of events.
                    eventList.close();

                } catch(PIMException pimExc) {
                    // TODO: Handle error on working with PIM.
                }
                catch(SecurityException secExc) {
                    // TODO: Handle error on access to PIM.
                }
                catch(Exception exc) {
                    // If unknown error was catched, exit from application.
                    exitMIDlet();
                }
            }

            /**
            * Checks PIM support.
            * @return - true if PIM is supported, false otherwise.
            */    
            private boolean checkPIMSupport() {
                String propValue = System.getProperty("microedition.pim.version");
                if(propValue != null) {
                    return true;
                } else {
                    return false;
                }
            }

            /**
            * Adds event to list of events. 
            * Gets data for event from addEventForm controls.
            */
            private void addEvent() {
                try {
                    // Get list of events.
                    EventList eventList = (EventList)PIM.getInstance().openPIMList(
                            PIM.EVENT_LIST, PIM.READ_WRITE);            

                    // Create new event.
                    Event event = eventList.createEvent();

                    // Get data from controls
                    if(eventList.isSupportedField(Event.SUMMARY) == true) {
                        String summary = summaryField.getString();
                        event.addString(Event.SUMMARY, PIMItem.ATTR_NONE, summary);
                    } else {
                        // At least summary must be supported.
                        eventList.close();
                        throw new Exception("Summary field for event is not supported");
                    }

                    if(eventList.isSupportedField(Event.START) == true) {
                        long startDate = startDateField.getDate().getTime();
                        event.addDate(Event.START, PIMItem.ATTR_NONE, startDate);
                    }

                    if(eventList.isSupportedField(Event.END) == true) {
                        long endDate = endDateField.getDate().getTime();
                        event.addDate(Event.END, PIMItem.ATTR_NONE, endDate);
                    }

                    if(eventList.isSupportedField(Event.NOTE) == true) {
                        String note = noteField.getString();
                        event.addString(Event.NOTE, PIMItem.ATTR_NONE, note);
                    }

                    if(eventList.isSupportedField(Event.LOCATION) == true) {
                        String location = locationField.getString();
                        event.addString(Event.LOCATION, PIMItem.ATTR_NONE, location);
                    }      

                    // Commit event.
                    event.commit();
                    // Close list of events.
                    eventList.close();

                    // Notify user that event was added
                    showAlert("Info", "Event was successfully added.");

                } catch(PIMException pimExc) {
                    // TODO: Handle error on working with PIM.
                    showAlert("PIMException", pimExc.getMessage());
                }
                catch(SecurityException secExc) {
                    // TODO: Handle error on access to PIM.
                    showAlert("SecurityException", secExc.getMessage());
                }
                catch(Exception exc) {
                    // TODO: Handle all other errors.
                    showAlert("Exception", exc.getMessage());
                }   
            }

            /**
            * Shows alert with specified title and text.
            * @param title - Title of alert.
            * @param message - text of alert.
            */
            private void showAlert(String title, String message) {
                Alert alert = new Alert(title);
                alert.setString(message);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }

            private void exitMIDlet() {
                notifyDestroyed();
            }

            public void commandAction(Command command, Displayable displayable) {
                if(command == cmdAddEvent) {
                    addEvent();
                }

                if(command == cmdExit) {
                    exitMIDlet();
                }
            }
        }
4

1 回答 1

3

试试这个,

// 在 MainMidlet 类中定义一个公共静态方法

private static Display display;  // declare static Display variable.

public static Display getDisplay () 
{
    return display;
}

现在从 AddCalendarEvent 类访问此方法,如下所示,

private void showAlert(String title, String message) 
{
    Alert alert = new Alert(title);
    alert.setString(message);
    alert.setTimeout(Alert.FOREVER);
    MainMidlet.getDisplay().setCurrent(alert);  // this line has been changed
}
于 2012-09-14T03:09:03.743 回答