mouseevent.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 678 行 · 第 1/2 页

JAVA
678
字号
           setNewModifiers();        } else if ((getModifiers() == 0) && (getModifiersEx() != 0 ||                   button != NOBUTTON)) {           setOldModifiers();        }     }    /**     * Constructs a <code>MouseEvent</code> object with the     * specified source component,     * type, modifiers, coordinates, and click count.     * <p>Note that passing in an invalid <code>id</code> results in     * unspecified behavior.     *     * @param source       the <code>Component</code> that originated the event     * @param id           the integer that identifies the event     * @param when         a long int that gives the time the event occurred     * @param modifiers    the modifier keys down during event (e.g. shift, ctrl,     *                     alt, meta)     *                     Either extended _DOWN_MASK or old _MASK modifiers     *                     should be used, but both models should not be mixed     *                     in one event. Use of the extended modifiers is     *                     preferred.     * @param x            the horizontal x coordinate for the mouse location     * @param y            the vertical y coordinate for the mouse location     * @param clickCount   the number of mouse clicks associated with event     * @param popupTrigger a boolean, true if this event is a trigger for a     *                     popup menu     */    public MouseEvent(Component source, int id, long when, int modifiers,        int x, int y, int clickCount, boolean popupTrigger) {        this(source, id, when, modifiers, x, y, clickCount, popupTrigger, NOBUTTON);        //super(source, id, when, modifiers);        //this.x = x;        //this.y = y;        //this.clickCount = clickCount;        //this.popupTrigger = popupTrigger;    }    /**     * Returns the horizontal x position of the event relative to the     * source component.     *     * @return x  an integer indicating horizontal position relative to     *            the component     */    public int getX() {        return x;    }    /**     * Returns the vertical y position of the event relative to the     * source component.     *     * @return y  an integer indicating vertical position relative to     *            the component     */    public int getY() {        return y;    }    /**     * Returns the x,y position of the event relative to the source component.     *     * @return a <code>Point</code> object containing the x and y coordinates     *         relative to the source component     *     */    public Point getPoint() {        int x;        int y;        synchronized (this) {            x = this.x;            y = this.y;        }        return new Point(x, y);    }    /**     * Translates the event's coordinates to a new position     * by adding specified <code>x</code> (horizontal) and <code>y</code>     * (vertical) offsets.     *     * @param x the horizontal x value to add to the current x     *          coordinate position     * @param y the vertical y value to add to the current y     coordinate position     */    public synchronized void translatePoint(int x, int y) {        this.x += x;        this.y += y;    }    /**     * Returns the number of mouse clicks associated with this event.     *     * @return integer value for the number of clicks     */    public int getClickCount() {        return clickCount;    }    /**     * Returns which, if any, of the mouse buttons has changed state.     *     * @returns one of the following constants:     * <code>NOBUTTON</code>,     * <code>BUTTON1</code>,     * <code>BUTTON2</code> or     * <code>BUTTON3</code>.     * @since 1.4     */     public int getButton() {     return button;     }    /**     * Returns whether or not this mouse event is the popup menu     * trigger event for the platform.     * <p><b>Note</b>: Popup menus are triggered differently     * on different systems. Therefore, <code>isPopupTrigger</code>     * should be checked in both <code>mousePressed</code>     * and <code>mouseReleased</code>     * for proper cross-platform functionality.     *     * @return boolean, true if this event is the popup menu trigger     *         for this platform     */    public boolean isPopupTrigger() {        return popupTrigger;    }    /**     * Returns a String describing the modifier keys and mouse buttons     * that were down during the event, such as "Shift", or "Ctrl+Shift".       * These strings can be localized by changing the awt.properties file.     *     * @param modifiers a modifier mask describing the modifier keys and     *                  mouse buttons that were down during the event     * @return string   a text description of the combination of modifier     *                  keys and mouse buttons that were down during the event     * @since 1.4     */    public static String getMouseModifiersText(int modifiers) {        StringBuffer buf = new StringBuffer();        if ((modifiers & InputEvent.ALT_MASK) != 0) {            buf.append(Toolkit.getProperty("AWT.alt", "Alt"));            buf.append("+");        }        if ((modifiers & InputEvent.META_MASK) != 0) {            buf.append(Toolkit.getProperty("AWT.meta", "Meta"));            buf.append("+");        }        if ((modifiers & InputEvent.CTRL_MASK) != 0) {            buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));            buf.append("+");        }        if ((modifiers & InputEvent.SHIFT_MASK) != 0) {            buf.append(Toolkit.getProperty("AWT.shift", "Shift"));            buf.append("+");        }        if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {            buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));            buf.append("+");        }        if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {            buf.append(Toolkit.getProperty("AWT.button1", "Button1"));            buf.append("+");        }        if ((modifiers & InputEvent.BUTTON2_MASK) != 0) {            buf.append(Toolkit.getProperty("AWT.button2", "Button2"));            buf.append("+");        }        if ((modifiers & InputEvent.BUTTON3_MASK) != 0) {            buf.append(Toolkit.getProperty("AWT.button3", "Button3"));            buf.append("+");        }        if (buf.length() > 0) {            buf.setLength(buf.length()-1); // remove trailing '+'        }        return buf.toString();    }    /**     * Returns a parameter string identifying this event.     * This method is useful for event-logging and for debugging.     *     * @return a string identifying the event and its attributes     */    public String paramString() {        String str;        switch (id) {        case MOUSE_PRESSED:            str = "MOUSE_PRESSED";            break;        case MOUSE_RELEASED:            str = "MOUSE_RELEASED";            break;        case MOUSE_CLICKED:            str = "MOUSE_CLICKED";            break;        case MOUSE_ENTERED:            str = "MOUSE_ENTERED";            break;        case MOUSE_EXITED:            str = "MOUSE_EXITED";            break;        case MOUSE_MOVED:            str = "MOUSE_MOVED";            break;        case MOUSE_DRAGGED:            str = "MOUSE_DRAGGED";            break;        case MOUSE_WHEEL:             str = "MOUSE_WHEEL";             break;        default:            str = "unknown type";        }        str += ",(" + x + "," + y + ")" + modifiers;        return str + ",clickCount=" + clickCount;    }    /**     * Sets new modifiers by the old ones.     * Also sets button. The mouse modifiers     * override overlaping key modifiers.     */        private void setNewModifiers() {     if ((modifiers & BUTTON1_MASK) != 0) {     modifiers |= BUTTON1_DOWN_MASK;     }     if ((modifiers & BUTTON2_MASK) != 0) {     modifiers |= BUTTON2_DOWN_MASK;     }     if ((modifiers & BUTTON3_MASK) != 0) {     modifiers |= BUTTON3_DOWN_MASK;     }     if (id == MOUSE_PRESSED     || id == MOUSE_RELEASED     || id == MOUSE_CLICKED)     {     if ((modifiers & BUTTON1_MASK) != 0) {     button = BUTTON1;     modifiers &= ~BUTTON2_MASK & ~BUTTON3_MASK;     if (id == MOUSE_PRESSED) {     modifiers &= ~BUTTON1_DOWN_MASK;     }     } else if ((modifiers & BUTTON2_MASK) != 0) {     button = BUTTON2;     modifiers &= ~BUTTON1_MASK & ~BUTTON3_MASK;     if (id == MOUSE_PRESSED) {     modifiers &= ~BUTTON2_DOWN_MASK;     }     } else if ((modifiers & BUTTON3_MASK) != 0) {     button = BUTTON3;     modifiers &= ~BUTTON1_MASK & ~BUTTON2_MASK;     if (id == MOUSE_PRESSED) {     modifiers &= ~BUTTON3_DOWN_MASK;     }     }     }     if ((modifiers & InputEvent.SHIFT_MASK) != 0) {     modifiers |= InputEvent.SHIFT_DOWN_MASK;     }     if ((modifiers & InputEvent.CTRL_MASK) != 0) {     modifiers |= InputEvent.CTRL_DOWN_MASK;     }     if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {     modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;     }     }    /**     * Sets old modifiers by the new ones.     */        private void setOldModifiers() {     if (id == MOUSE_PRESSED     || id == MOUSE_RELEASED     || id == MOUSE_CLICKED)     {     switch(button) {     case BUTTON1:     modifiers |= BUTTON1_MASK;     break;     case BUTTON2:     modifiers |= BUTTON2_MASK;     break;     case BUTTON3:     modifiers |= BUTTON3_MASK;     break;     }     } else {     if ((modifiers & BUTTON1_DOWN_MASK) != 0) {     modifiers |= BUTTON1_MASK;     }     if ((modifiers & BUTTON2_DOWN_MASK) != 0) {     modifiers |= BUTTON2_MASK;     }     if ((modifiers & BUTTON3_DOWN_MASK) != 0) {     modifiers |= BUTTON3_MASK;     }     }     if ((modifiers & SHIFT_DOWN_MASK) != 0) {     modifiers |= SHIFT_MASK;     }     if ((modifiers & CTRL_DOWN_MASK) != 0) {     modifiers |= CTRL_MASK;     }     if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {     modifiers |= ALT_GRAPH_MASK;     }     }    /**     * Sets new modifiers by the old ones. The mouse modifiers     * override overlaping key modifiers.     * @serial     */    private void readObject(ObjectInputStream s)        throws IOException, ClassNotFoundException {        s.defaultReadObject();      	if (getModifiers() != 0 && getModifiersEx() == 0) {           setNewModifiers();        }    }}

⌨️ 快捷键说明

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