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

📄 form.java

📁 j2me设计的界面包
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        if (previousForm != null) {
            previousForm.tint = false;

            if (previousForm instanceof Dialog) {
                if (!((Dialog) previousForm).isDisposed()) {
                    Display.getInstance().setCurrent(previousForm);
                }
            } else {
                Display.getInstance().setCurrent(previousForm);
            }

            // enable GC to cleanup the previous form if no longer referenced
            previousForm = null;
        }
    }

    /**
     * @inheritDoc
     */
    void repaint(Component cmp) {
        if (isVisible()) {
            if (current != null) {
                Component c = cmp;
                while (c != null) {
                    if (c == current.getContents()) {
                        Display.getInstance().repaint(cmp);
                        return;
                    }
                    c = c.getParent();
                }
            } else {
                Display.getInstance().repaint(cmp);
            }
        }
    }

    /**
     * @inheritDoc
     */
    public final Form getComponentForm() {
        return this;
    }

    /**
     * Invoked by display to hide the menu during transition
     * 
     * @see restoreMenu
     */
    void hideMenu() {
        super.removeComponent(menuBar);
    }

    /**
     * Invoked by display to restore the menu after transition
     * 
     * @see hideMenu
     */
    void restoreMenu() {
        if (menuBar.getParent() == null) {
            super.addComponent(BorderLayout.SOUTH, menuBar);
        }
    }

    /**
     * Sets the focused component and fires the appropriate events to make it so
     * 
     * @param focused the newly focused component or null for no focus
     */
    public void setFocused(Component focused) {
        if (this.focused == focused && focused != null) {
            this.focused.repaint();
            return;
        }
        if (this.focused != null) {
            this.focused.setFocus(false);
            this.focused.fireFocusLost();
            fireFocusLost(this.focused);
            if (this.focused.getParent() != null) {
                this.focused.repaint();
            }
        }
        if (focused != null) {
            focused.setFocus(true);
            focused.fireFocusGained();
            fireFocusGained(focused);
            focused.repaint();
        }
        this.focused = focused;
    }

    /**
     * Returns the current focus component for this form
     * 
     * @return the current focus component for this form
     */
    public Component getFocused() {
        return focused;
    }

    /**
     * @inheritDoc
     */
    public void keyPressed(int keyCode) {
        int game = Display.getInstance().getGameAction(keyCode);
        if (keyCode == leftSK || (keyCode == rightSK || keyCode == rightSK2) || keyCode == backSK || keyCode == clearSK ||
                (Display.getInstance().isThirdSoftButton() && game == Display.GAME_FIRE)) {
            menuBar.keyPressed(keyCode);
            return;
        }

        //Component focused = focusManager.getFocused();
        if (focused != null) {
            focused.keyPressed(keyCode);
            if (focused.handlesInput()) {
                return;
            }
            if (focused.getComponentForm() == this) {
                if (focused != null && focused.handlesInput()) {
                    return;
                }
                //if the arrow keys have been pressed update the focus.
                updateFocus(Display.getInstance().getGameAction(keyCode));
            } else {
                initFocused();
            }
        } else {
            initFocused();
        }

    }

    /**
     * @inheritDoc
     */
    public void keyReleased(int keyCode) {
        int game = Display.getInstance().getGameAction(keyCode);
        if (keyCode == leftSK || (keyCode == rightSK || keyCode == rightSK2) || keyCode == backSK || keyCode == clearSK ||
                (Display.getInstance().isThirdSoftButton() && game == Display.GAME_FIRE)) {
            menuBar.keyReleased(keyCode);
            return;
        }

        //Component focused = focusManager.getFocused();
        if (focused != null) {
            if (focused.getComponentForm() == this) {
                focused.keyReleased(keyCode);
            }
        }

        // prevent the default action from stealing the behavior from the popup/combo box...
        if (game == Display.GAME_FIRE && current == null) {
            Command defaultCmd = getDefaultCommand();
            if (defaultCmd != null) {
                defaultCmd.actionPerformed(new ActionEvent(defaultCmd, keyCode));
                actionCommandImpl(defaultCmd);
            }
        }
        fireKeyEvent(keyListeners, keyCode);
        fireKeyEvent(gameKeyListeners, game);
    }

    private void fireKeyEvent(Hashtable keyListeners, int keyCode) {
        if (keyListeners != null) {
            Vector listeners = (Vector) keyListeners.get(new Integer(keyCode));
            if (listeners != null) {
                ActionEvent evt = new ActionEvent(this, keyCode);
                for (int iter = 0; iter < listeners.size(); iter++) {
                    ((ActionListener) listeners.elementAt(iter)).actionPerformed(evt);
                    if (evt.isConsumed()) {
                        return;
                    }
                }
            }
        }
    }

    /**
     * @inheritDoc
     */
    public void keyRepeated(int keyCode) {
        //Component focused = focusManager.getFocused();
        if (focused != null) {
            focused.keyRepeated(keyCode);

            // this has issues in the WTK
            if (!focused.handlesInput()) {
                keyPressed(keyCode);
                keyReleased(keyCode);
            }
        } else {
            keyPressed(keyCode);
            keyReleased(keyCode);
        }
    }

    /**
     * @inheritDoc
     */
    public void pointerPressed(int x, int y) {

        //if there is no popup on the screen an click is relevant to the menu bar.
        if (focusCycleRoot.equals(contentPane) && menuBar.contains(x, y)) {
            Component cmp = menuBar.getComponentAt(x, y);
            if (cmp != null) {
                cmp.pointerPressed(x, y);
            }
            return;
        }

        //if the visible cycle root is a popup, and the user clicked out of the popup bounds
        //make the popup invisible.
        if (!focusCycleRoot.equals(contentPane) && !focusCycleRoot.contains(x, y)) {
            if (current != null) {
                current.setVisible(false);
            }
            return;
        }

        if (focusCycleRoot instanceof Container) {
            Component cmp = ((Container) focusCycleRoot).getComponentAt(x, y);
            if (cmp != null && cmp.isFocusable()) {
                setFocused(cmp);
                cmp.pointerPressed(x, y);
                cmp.repaint();
            }
        } else {
            focusCycleRoot.pointerPressed(x, y);
            focusCycleRoot.repaint();
        }
    }

    /**
     * @inheritDoc
     */
    public void pointerDragged(int x, int y) {
        if (focusCycleRoot instanceof Container) {
            Component cmp = ((Container) focusCycleRoot).getComponentAt(x, y);
            if (cmp != null) {
                if (cmp.isFocusable()) {
                    setFocused(cmp);
                }
                cmp.pointerDragged(x, y);
                cmp.repaint();
            }
        } else {
            focusCycleRoot.pointerDragged(x, y);
            focusCycleRoot.repaint();
        }
    }

    /**
     * Returns true if there is only one focusable member in this form. This is useful
     * so setHandlesInput would always be true for this case.
     * 
     * @return true if there is one focusable component in this form, false for 0 or more
     */
    public boolean isSingleFocusMode() {
        initFocusDown();
        return focusDownSequence.size() == 1;
    }
    
    /**
     * @inheritDoc
     */
    public void pointerReleased(int x, int y) {
        if (focusCycleRoot.equals(contentPane) && menuBar.contains(x, y)) {
            Component cmp = menuBar.getComponentAt(x, y);
            if (cmp != null) {
                cmp.pointerReleased(x, y);
            }
            return;
        }

        if (!focusCycleRoot.equals(contentPane) && !focusCycleRoot.contains(x, y)) {
            focusCycleRoot = contentPane;
            if (lastFocused != null) {
                setFocused(lastFocused);
            } else {
                initFocused();
            }
            return;
        }

        // special case for popups which are not children of the content pane
        if (current == null) {
            contentPane.pointerReleased(x, y);
        } else {
            if (focusCycleRoot instanceof Container) {
                Component cmp = ((Container) focusCycleRoot).getComponentAt(x, y);
                if (cmp != null && cmp.isFocusable()) {
                    setFocused(cmp);
                    cmp.pointerReleased(x, y);
                    cmp.repaint();
                }
            } else {
                focusCycleRoot.pointerReleased(x, y);
                focusCycleRoot.repaint();
            }
        }
    }

    /**
     * @inheritDoc
     */
    public void setScrollableY(boolean scrollableY) {
        getContentPane().setScrollableY(scrollableY);
    }

    /**
     * @inheritDoc
     */
    public void setScrollableX(boolean scrollableX) {
        getContentPane().setScrollableX(scrollableX);
    }

    /**
     * Adds a command to the menu bar softkeys or into the menu dialog, 
     * this version of add allows us to place a command in an arbitrary location.
     * This allows us to force a command into the softkeys when order of command
     * addition can't be changed.
     * 
     * @param cmd the Form command to be added
     * @param offset position in which the command is added
     */
    public void addCommand(Command cmd, int offset) {
        menuBar.addCommand(cmd, offset);
    }

    /**
     * A helper method to check the amount of commands within the form menu
     */
    public int getCommandCount() {
        return menuBar.getCommandCount();
    }

    /**
     * Returns the command occupying the given index
     * 
     * @param index offset of the command
     * @return the command at the given index
     */
    public Command getCommand(int index) {
        return menuBar.getCommand(index);
    }

    /**
     * Adds a command to the menu bar softkeys.
     * The Commands are placed in the order they are added.
     * If the Form has 1 Command it will be placed on the right.
     * If the Form has 2 Commands the first one that was added will be placed on
     * the right and the second one will be placed on the left.
     * If the Form has more then 2 Commands the first one will stay on the left
     * and a Menu will be added with all the remain Commands.
     * 
     * @param cmd the Form command to be added
     */
    public void addCommand(Command cmd) {
        menuBar.addCommand(cmd);
    }

    /**
     * Removes the command from the menu bar softkeys
     * 
     * @param cmd the Form command to be removed
     */
    public void removeCommand(Command cmd) {
        menuBar.removeCommand(cmd);
    }

    private void updateFocus(int gameAction) {
        Component focused = getFocused();
        switch (gameAction) {
            case Display.GAME_DOWN: {
                if (focused.getNextFocusDown() != null) {
                    focused = focused.getNextFocusDown();
                } else {
                    initFocusDown();
                    int i = focusDownSequence.indexOf(focused) + 1;
                    if (i == focusDownSequence.size()) {
                        i = 0;
                    }
                    focused = (Component) focusDownSequence.elementAt(i);
                }
                break;

⌨️ 快捷键说明

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