simplecheckbox.java

来自「j2me下的1套UI框架.包含j2me开发中会应用的各种低级组件」· Java 代码 · 共 87 行

JAVA
87
字号
package com.jmobilecore.ui.core;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Font;

/**
 * A check box is a graphical component that can be in either an
 * "on" (<code>true</code>) or "off" (<code>false</code>) state.
 * Clicking on a check box changes its state from
 * "on" to "off," or from "off" to "on."
 * This object cannot be included into <code>CheckboxGroup</code> object
 *
 * @author Greg Gridin
 */
public class SimpleCheckbox extends Label {

    /**
     * The state of the <code>Checkbox</code>.
     */
    public boolean state;

    /**
     * Creates a check box with the specified label
     * and sets the specified state.
     *
     * @param     label   a string label for this check box,
     *                        or <code>null</code> for no label
     * @param     state    the initial state of this check box
     */
    public SimpleCheckbox(String label, boolean state) {
        this(label, Style.TEXT_FONT, state);
    }

    /**
     * Creates a check box with the specified label
     * and sets the specified state.
     *
     * @param     label   a string label for this check box,
     *                        or <code>null</code> for no label
     * @param     state    the initial state of this check box
     */
    public SimpleCheckbox(String label, Font font, boolean state) {
        super(label, font, Label.LEFT);
        this.state = state;
        focusable = true;
    }

    /**
     * Paints the checkbox to the screen.
     *
     * @param g The Graphics object
     */
    public void paint(Graphics g) {

        super.paint(g);

        int y = screenY + Style.V_GAP;
        final int sideSize = getHeight() - 3;
        int x;
        if (alignment == LEFT || alignment == CENTER ) {
            x = getWidth()- sideSize - 1 - Style.H_GAP;
        } else /*if (alignment == RIGHT)*/ {
            x = Style.H_GAP;
        }
        g.drawRect(x, y, sideSize, sideSize);
        if (state) {
            g.drawLine(x, y, x + sideSize - 1, y + sideSize - 1);
            g.drawLine(x + sideSize, y, x, y + sideSize);
        }
    }

    /**
     * Responds to key presses when the button has focus.
     *
     * @param keyCode The pressed key.
     */
    public boolean keyPressed(int keyCode) {
        if (keyCode == PlatformCanvas.KEY_ENTER) {
            state = !state;
            parentScreen.repaint();
            return true;
        }
        return super.keyPressed(keyCode);
    }

} // class Checkbox

⌨️ 快捷键说明

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