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

📄 textfield.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @throws ArrayIndexOutOfBoundsException if offset and length do not     * specify a valid range within the data array     * @throws IllegalArgumentException if the resulting contents are illegal     * for the current <a href="TextField.html#constraints">input     * constraints</a>     * @throws IllegalArgumentException if the insertion would exceed the     * current maximum capacity     * @throws NullPointerException if <code>data</code> is <code>null</code>     */    public void insert(char[] data, int offset, int length, int position)  {        synchronized (Display.LCDUILock) {            sD.insert(data, offset, length, position);        }    }    /**     * <p>Deletes characters from the TextField.</p>     *     * @param offset the beginning of the region to be deleted     * @param length the number of characters to be deleted     *     * @throws StringIndexOutOfBoundsException if offset and length do not     * specify a valid range within the contents of the TextField     */    public void delete(int offset, int length) {        synchronized (Display.LCDUILock) {            int deltaHeight = sD.deleteChars(offset, length);            contentHeight += deltaHeight;            height        += deltaHeight;            contentChanged(0, 0, deltaHeight);        }    }    /**     * Returns the maximum size (number of characters) that can be     * stored in this     * TextField.     * @return the maximum size in characters     *     * @see #setMaxSize     */    public int getMaxSize() {        synchronized (Display.LCDUILock) {            return sD.buffer.length;        }    }    /**     * Sets the maximum size (number of characters) that can be     * contained in this     * TextField. If the current contents of the TextField are larger than     * maxSize, the contents are truncated to fit.     *     * @param maxSize the new maximum size     *     * @return assigned maximum capacity - may be smaller than requested.     * @throws IllegalArgumentException if maxSize is zero or less.     *     * @see #getMaxSize     */    public int setMaxSize(int maxSize) {        synchronized (Display.LCDUILock) {            int oldHeight = height;            sD.setMaxSize(maxSize);            layoutChanged(oldHeight);            return sD.buffer.length;        }    }    /**     * Gets the number of characters that are currently stored in this     * TextField.     * @return number of characters in the TextField     */    public int size() {        synchronized (Display.LCDUILock) {            if (sD.buffer == null) {                return 0;            } else {                return sD.numChars;            }        }    }    /**     * Gets the current input position.     * For some UIs this may block some time and ask the user about     * the intended caret      * position, on some UIs may just return the caret position.     * @return the current caret position, 0 if in the beginning.     */    public int getCaretPosition() {        synchronized (Display.LCDUILock) {            return sD.numChars;        }    }    /**     * Sets the input constraints of the TextField. If the the current contents     * of the TextField do not match the new constraints, the contents are     * set to empty.     *     * @param constraints see <a href="#constraints">input constraints</a>     *     * @throws IllegalArgumentException if constraints is not any of the ones     * specified in <a href="TextField.html#constraints">input constraints</a>     *     * @see #getConstraints     */    public void setConstraints(int constraints)  {        synchronized (Display.LCDUILock) {            int oldHeight = height;            sD.setConstraints(constraints);            sD.setMaxSize(sD.buffer.length);            layoutChanged(oldHeight);        }    }    /**     * Get the current input constraints of the TextField.     *     * @return the current constraints value (see     * <a href="#constraints">input constraints</a>)     *     * @see #setConstraints     */    public int getConstraints() {        synchronized (Display.LCDUILock) {            return sD.constraints;        }    }    /**     * Sets the label of the Item. If label is null, specifies that this     * item has no label.     * @param label the label string     */    public void setLabel(String label) {        synchronized (Display.LCDUILock) {            super.setLabel(label);            int oldHeight = height;            ((StringLayout)layouts[0]).setString(label);            layoutChanged(oldHeight);        }    }    // package private    /**     * setWidth allows the width of the TextField to be set     *     * @param width The width to set the TextField to     * @return int The new height based on the new width     */    int setWidth(int width) {        if (this.width != width) {            this.width = width;            doLayout();        }        return height;    }    /**     * Get the height of this TextField     *     * @return int Return the height in pixels of this TextField     */    int getHeight() {        return height;    }    /**     * Paint this TextField     *     * @param g The Graphics object to paint to     */    void paint(Graphics g) {        int translateX = 0;        int translateY = 0;        g.setColor(Display.ERASE_COLOR);        g.fillRect(0, 0, width, height);        // draw label        if (horLayout) {            g.translate(0, 4);            layouts[0].paint(g, false, false);            translateX += labelWidth;            g.translate(labelWidth, -4);        } else {            layouts[0].paint(g, false, false);            int labelHeight = layouts[0].getHeight();            g.translate(0, labelHeight);            translateY += labelHeight;        }        // draw box        if (hasFocus()) {            g.setColor(0x00000000);        } else {            g.setColor(0x00AFAFAF);        }        g.drawRect(2, 2, tfWidth + 4, contentHeight + 4);        g.translate(4, 4);        translateX += 4;        translateY += 4;        // need to special case password paint        // to show last char during input (otherwise ECHO char will be drawn)        sD.policy.paint(g, sD.buffer, sD.numChars, false, 0,                        (sD.policy instanceof PassTextPolicy));        g.translate(-translateX, -translateY);    }        /**     * ONLY used by TextEditor. Content of text field might have changed;     * height and contentHeight variables have to be updated. It is the     * caller's responsibility to notify the Form's ItemStateChangeListener.     *     * @param newContentHeight The new height of the content     */    void saveHeight(int newContentHeight) {        if (!horLayout) {            int deltaHeight = newContentHeight - contentHeight;            contentHeight = newContentHeight;            height += deltaHeight;            contentChanged(0, 0, deltaHeight);        }    }    /**     * Select this TextField     *     * @return boolean Always returns false, invokes the TextEditor     */    boolean select() {        textEditor.invoke(getOwner(), this, 0, false);        return false;    }    /**     * Handle a key pressed event     *     * @param keyCode The code of the key which was pressed     * @return boolean Always returns false, may invoke the editor     */    boolean keyPressed(int keyCode) {        // System.out.println("inside textfields keyPressed for "+keyCode);        if (Display.getSystemKey(keyCode) == EventHandler.SYSTEM_KEY_SEND) {            if (sD.policy instanceof PhonePolicy) {                PhoneDial.call(getString());            }            return false;        }        // fix for delete as delete cause problems on invoking textbox.        // If delete is pressed in TextField then it deletes one character        // and then invokes the TextBox. Doesn't do anything for long delete.        if (Display.getSystemKey(keyCode) == EventHandler.SYSTEM_KEY_CLEAR) {            // delete case            if (sD.numChars > 0) {                delete(sD.numChars - 1, 1);            }            keyCode = 0; // non action        }        textEditor.invoke(getOwner(), this, keyCode, false);        // NOTE: The text editor will notify the item state listener        // itself upon a 'SAVE' command, so we always return false        // from here        return false;    }    /**     * Method to allow for keyboard entry     *     * @param c The character entered from the keyboard     */    void keyTyped(char c) {        textEditor.invoke(getOwner(), this, c, true);    }    // private    /**     * Layout the contents of this TextField     */    private void doLayout() {        labelWidth = 0;        height     = layouts[0].setWidth(width);        tfWidth       = sD.getMaxWidth(width - SPACER);        contentHeight = sD.setWidth(tfWidth);        horLayout = false;        // Horizontal layout could be possible only if label is one line high        // and content is one line hight (the same font is used in both cases)        if (height <= Screen.CONTENT_HEIGHT &&                contentHeight <= Screen.CONTENT_HEIGHT) {            String label = getLabel();            if (label != null) {                labelWidth = Screen.CONTENT_FONT.stringWidth(label);            }            // See if label and text field itself will fit on one line            if (labelWidth + tfWidth + SPACER < width) {                horLayout = true;                // contentHeight is at least of a lineHeight                height = contentHeight + SPACER;                return;            }        }        height += (contentHeight + SPACER);    }    /**     * Notify this TextField that its layout has changed     *     * @param oldHeight The old height of this TextField     */    private void layoutChanged(int oldHeight) {        if (initLayoutDone()) {            doLayout();            contentChanged(0, 0, height - oldHeight);        }    }}

⌨️ 快捷键说明

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