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

📄 defaulteventhandler.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
字号:
/* * @(#)DefaultEventHandler.java	1.36 01/08/16 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information").  You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package com.sun.midp.lcdui;import com.sun.midp.midlet.Scheduler;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.Display;import javax.microedition.io.*;import java.io.*;import com.sun.midp.io.InternalConnector;import com.sun.midp.Configuration;/** * This is the default event handler for the LCDUI. */public class DefaultEventHandler implements EventHandler {    /**     * Schedule a private timer of the given type.     *     * @param type the type of timer event (SCREEN, REPAINT, CALLED_SERIALLY)     * @param milliseconds time from now to trigger this event     */    public native void scheduleTimer(int type, int milliseconds);    /**     * Get the system-specific key code corresponding to the given gameAction.     *     * @param gameAction A game action     * @return The keyCode associated with that action     */    public native int  getKeyCode(int gameAction);    /**     * Get the abstract gameAction corresponding to the given keyCode.     *     * @param keyCode A system-specific keyCode     * @return gameAction The abstract game action associated with the keyCode     */    public native int  getGameAction(int keyCode);    /**     * Get the abstract system key that corresponds to keyCode.     *     * @param keyCode A system-specific keyCode     * @return The SYSTEM_KEY_ constant for this keyCode, or 0 if none     */    public native int  getSystemKey(int keyCode);    /**     * Get the informative key string corresponding to the given keyCode.     *     * @param keyCode A system-specific keyCode     * @return a string name for the key, or null if no name is available     */    public native String getKeyName(int keyCode);    /**     * Set the current set of active Abstract Commands.     *     * @param commands The list of Commands that should be active     * @param numCommands The number of commands in the list     */    public native void updateCommandSet(Command[] commands, int numCommands);    /**     * This is the private default DisplayAccess object that does nothing      * with incoming events.     */    private static DisplayAccess noRecipient = new DisplayAccess() {            /**             * Get the Display object that is associated with this              * DisplayAccess             *             * @return null.             */	    public Display getDisplay() { return null; }            /**             * Does nothing             *             * @param id The integer id of the Command that fired (as returned             *           by Command.getID())             */	    public void commandAction(int id) { }            /**             * Does nothing             *             * @param type The type of event (press, release or drag)             * @param x The x coordinate of the location of the pen             * @param y The y coordinate of the location of the pen             */	    public void pointerEvent(int type, int x, int y) { }            /**             * Does nothing             *             * @param type    The type of event (press, release or repeat)             * @param keyCode The key code for the key that registered the event             */	    public void keyEvent(int type, int keyCode) { }            /**             * Does nothing             *             * @param type the type of timer event (SCREEN,              *             REPAINT, CALLED_SERIALLY)             */	    public void timerEvent(int type) { }            /**             * Does nothing             */	    public void suspendPainting() { }            /**             * Does nothing             */	    public void resumePainting() { }            /**             * Does nothing             *             * @param hasForeground true if the Display should be put in             *                      the foreground.             */	    public void setForeground(boolean hasForeground) { }            /**             * Does nothing             *             * @param str The string from the input method             */	    public void inputMethodEvent(String str) { }            /**             * Return false always             *             * @return false             */	    public boolean hasCurrent() { return false; }	};    /**     * If shouldReceiveEvents == true, make da the object that     * will receive events.  If shouldReceiveEvents == false and the     * current recipient is da, clear the event recipient.     *     * @param da The DisplayAccess object to receive events in the future.     * @param shouldReceiveEvents flag to indicate if the DisplayAccess     *                            object should receive events or not.     */    public void setEventRecipient(DisplayAccess da,                                   boolean shouldReceiveEvents) {        if (shouldReceiveEvents) {            current = da;        } else if (current == da) {            current = noRecipient;        }    }    /**     * The constructor for the default event handler for LCDUI.     */    public DefaultEventHandler() {        current = noRecipient;        try {            // queue has to be declared final because it is used in the inner            // class in eventThread.            final Events queue = new Events();            // connect to the native event queue            queue.open();            /**             * create a new thread to read the events.             */            eventThread = new Thread() {                /**                 * Read the event queue                 */                public void run() {                    for (;;) {                        try {                            /* all events start with an event ID */                            int evt = queue.readInt();                            // This is a distinguishing type, e.g.                            // PRESSED, RELEASED, etc.                            int type;                            switch (evt) {                                case KEY_EVENT: // ID, type, keyCode                                    type     = queue.readInt();                                    if (type == IME) {                                        String str = queue.readUTF();                                        current.inputMethodEvent(str);                                        break;                                    }                                    int code = queue.readInt();                                    /*                                    * KEY_END release should let MIDlet                                    * manager kill the current MIDlet.                                    */                                    if (getSystemKey(code) == SYSTEM_KEY_END                                            && type == RELEASED) {                                        Scheduler.getScheduler()                                            .notifyEnd(current);                                        break;                                    }                                    if (inMenu) {                                        inMenu = menuKeyEvent(type, code);                                    } else {                                        current.keyEvent(type, code);                                    }                                    break;                                case PEN_EVENT: // ID, type, x, y                                    type  = queue.readInt();                                    int x = queue.readInt();                                    int y = queue.readInt();                                    if (inMenu) {                                        inMenu = menuPointerEvent(type, x, y);                                    } else {                                        current.pointerEvent(type, x, y);                                    }                                    break;                                case COMMAND_EVENT: // ID, commandIndex                                    type = queue.readInt();                                    if (type == MENU_REQUESTED) {                                        current.suspendPainting();                                        paintMenu();                                        inMenu = true;                                        break;                                    } else if (type == MENU_DISMISSED) {                                        if (inMenu) {                                            current.resumePainting();                                        }                                        inMenu = false;                                        break;                                    } else if (type >= 0) { // command seen                                        if (inMenu) {                                            current.resumePainting();                                        }                                        inMenu = false;                                        current.commandAction(type);                                    }                                    break;                                case TIMER_EVENT: // ID, type                                     type = queue.readInt();                                    current.timerEvent(type);                                    break;                                default:                                    throw new Error("" + evt);                            }                        } catch (Throwable t) {                            // REMIND: This used to throw an uncaught Error,                            // should it still or should we just let things                            // keep running??                            // event loop should never be allowed to die                            t.printStackTrace();                            throw new Error(t.getMessage());                        }                    }                }            };            eventThread.start();        } catch (IOException x) {            throw new Error();        }    }    /**     * Returns true if the current thread is the EventHandler's dispatch      * thread.     *     * @return true if the current thread is this EventHandler's     *         dispatch thread     */    public boolean isDispatchThread() {        return (Thread.currentThread() == eventThread);    }    /**     * Called to force the event handler to clear whatever system screen     * has interrupted the current Displayable and allow the foreground     * Display to resume painting.     */    public void clearSystemScreen() {        inMenu = false;        dismissMenu();    }    /**     * The current DisplayAccess object handling the events.     */    private DisplayAccess current;    /**     * The default event thread     */    private Thread eventThread;    // These are for handling Abstract Command menus.    /**      * This field is true if the current display is the menu screen.     */    private boolean inMenu = false;    /**     * Native method to draw the command menu on the screen      */    private native void paintMenu();    /**     * Native method to dismiss the current menu in the case of setCurrent()     * being called while the Display is suspended by a system screen.     */    private native void dismissMenu();    /**     * Handle the key event when the menu is the current display.     *     * @param  type one of PRESSED, RELEASED, or REPEATED     * @param  code the key code of the key event     * @return true if the event is the current display is the menu screen.     */    private native boolean menuKeyEvent(int type, int code);    /**     * Handle the pointer event when the menu is the current display.     *     * @param  type one of PRESSED, RELEASE, or DRAGGED     * @param  x    the x co-ordinate of the pointer event      * @param  y    the y co-ordinate of the pointer event      * @return true if the event is the current display is the menu screen.     */    private native boolean menuPointerEvent(int type, int x, int y);}

⌨️ 快捷键说明

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