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

📄 checkbox.java

📁 开发j2me 手机程序必须的用到的。文件较小
💻 JAVA
字号:
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."
 *
 * @author Greg Gridin
 */
public class Checkbox extends Label {

    /**
     * The check box group.
     * This field can be null indicating that the checkbox
     * is not a group checkbox.
     */
    public CheckboxGroup group;

    /**
     * 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 Checkbox(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 Checkbox(String label, Font font, boolean state) {
        super(label, font, Label.LEFT);
        this.state = state;
        focusable = true;
    }

    /**
     * Constructs a Checkbox with the specified label, set to the
     * specified state, and in the specified check box group.
     *
     * @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.
     * @param group a check box group for this check box,
     *              or <code>null</code> for no group.
     */
    public Checkbox(String label, Font font, boolean state, CheckboxGroup group) {
        this(label, font, state);
        this.group = group;
        if (state && (group != null)) {
            group.setSelectedCheckbox(this);
        }
    }

    /**
     * Sets the state of this check box to the specified state.
     * The boolean value <code>true</code> indicates the "on" state,
     * and <code>false</code> indicates the "off" state.
     *
     * @param state the boolean state of the check box
     */
    public void setState(boolean state) {
        CheckboxGroup group = this.group;
        if (group != null) {
            if (state) {
                group.setSelectedCheckbox(this);
            } else if (group.selectedCheckbox == this) {
                state = true;
            }
        }
        this.state = state;
    }

    /**
     * Sets this check box's group to be the specified check box group.
     * If this check box is already in a different check box group,
     * it is first taken out of that group.
     * If the state of this check box is <code>true</code> and the new
     * group already has a check box selected, this check box's state
     * is changed to <code>false</code>.
     *
     * @param g the new check box group, or <code>null</code>
     *          to remove this check box from any check box group.
     */
    public void setCheckboxGroup(CheckboxGroup g) {
        CheckboxGroup oldGroup;
        boolean oldState;

        if (this.group == g) return;
        oldGroup = this.group;
        oldState = state;
        this.group = g;
        
        if (this.group != null && state) {
            if (this.group.selectedCheckbox != null) {
                setState(false);
            } else {
                this.group.setSelectedCheckbox(this);
            }
        }

        /* Locking check box below could cause deadlock with
         * CheckboxGroup's setSelectedCheckbox method.
         *
         * Fix for 4726853 by kdm@sparc.spb.su
         * Here we should check if this check box was selected
         * in the previous group and set selected check box to
         * null for that group if so.
         */
        if (oldGroup != null && oldState) {
            oldGroup.setSelectedCheckbox(null);
        }
    }

    /**
     * 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) {
            setState(!state);
            parentScreen.repaint();
            return true;
        }
        return super.keyPressed(keyCode);
    }

} // class Checkbox

⌨️ 快捷键说明

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