?? button.java
字號(hào):
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;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -