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

📄 button.java

📁 为解压缩文件
💻 JAVA
字号:

package com.j2medev.numbergame;
import javax.microedition.lcdui.*;

public class Button extends Area
{

    private String label;

    private boolean selected;

    private ButtonListener listener;

    private boolean modifiable = true;
    
    private int marginx = 4;
    
    private int marginy = 4;

    public Button(String label, int x, int y)
    {
        this(label, x, y, 0, 0, null);
    }

    public Button(String label, int x, int y, Font f)
    {
        this(label, x, y, 0, 0, f);
    }

    public Button(String label, int x, int y, int w, int h)
    {
        this(label, x, y, w, h, null);
    }

    public Button(String label, int x, int y, int w, int h, Font f)
    {
        super(x, y, w, h, f);

        if (label == null)
            label = "";

        int tw = calcMinWidth(label, getFont());
        int th = calcMinHeight(label, getFont());

        if (tw > w)
            this.w = tw;
        if (th > h)
            this.h = th;

        this.label = label;
    }

    public void setModifiable(boolean flag)
    {
        this.modifiable = flag;
    }

    public static int calcMinWidth(String text, Font f)
    {
        return f.stringWidth(text) + 8;
    }

    public static int calcMinHeight(String text, Font f)
    {
        return f.getHeight() + 8;
    }

    public String getLabel()
    {
        return label;
    }
    
    public void setMargin(int x,int y)
    {
        this.marginx = x;
        this.marginy = y;
    }
    protected void paintArea(Graphics g, boolean hasFocus)
    {
        g.setStrokeStyle(Graphics.SOLID);
        g.drawRect(x, y, w - 1, h - 1);

       
        
        if (selected)
        {
            g.setColor(getForeColor());
            g.fillRect(x, y, w - 1, h - 1);
            g.setColor(getBackColor());
        } else if (hasFocus)
        {
            g.setStrokeStyle(Graphics.DOTTED);
            g.drawRect(x + 3, y + 3, w - 7, h - 7);
            g.setStrokeStyle(Graphics.SOLID);
        }

        g.drawString(label, x + marginx, y + marginy, Graphics.TOP | Graphics.LEFT);
    }

    public void keyPressed(int keyCode)
    {
        int num = keyCode - 48;
        if (num >= 0)
        {
            if (modifiable)
            {
                this.label = num + "";
            }
            repaintArea(this, true);
            moveFocus(true);
            return;
        }

        int action = getGameAction(keyCode);
        switch (action)
        {
        case UP:
        case LEFT:
            moveFocus(false);
            break;
        case DOWN:
        case RIGHT:
            moveFocus(true);
            break;
        case FIRE:
            if (listener != null)
            {
                listener.buttonPressed(this);
            }
            break;
        }

    }

    public void setLabel(String s)
    {
        this.label = s;
    }

    public void keyReleased(int keyCode)
    {

    }

    public void setListener(ButtonListener listener)
    {
        this.listener = listener;
    }
}

⌨️ 快捷键说明

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