?? mybutton.java
字號(hào):
package demo;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
public class MyButton {
//定義一組表示按鈕種類的數(shù)值
public static final int BUTTON_START = 0; //開始按鈕
public static final int BUTTON_RESET = 1; //復(fù)位按鈕
public static final int BUTTON_CONTINUE = 2; //繼續(xù)按鈕
public static final int BUTTON_HELP = 3; //幫助按鈕
public static final int BUTTON_SAVE = 4; //保存按鈕
public static final int BUTTON_EXIT = 5; //退出按鈕
public static final int BUTTON_TYPE_NUM = 6; //按鈕種類
private Sprite m_ButtonSp; //按鈕精靈圖像
private int m_aIndex[]; //按鈕的種類
private boolean m_abShow[]; //按鈕是否顯示的標(biāo)志
private int m_nShowNum = 0; //當(dāng)前顯示的按鈕數(shù)量
private int m_nFocusType = -1; //高亮按鈕類型
//構(gòu)造方法
//參數(shù)width,height設(shè)置按鈕精靈的寬和高
//6個(gè)Index參數(shù)分別是各類型的按鈕在精靈圖像中的索引,-1表示沒有這種按鈕的圖像
public MyButton(int width, int height, int startIndex, int resetIndex,
int continueIndex, int helpIndex, int saveIndex, int exitIndex){
m_aIndex = new int[BUTTON_TYPE_NUM];
m_abShow = new boolean[BUTTON_TYPE_NUM];
m_nShowNum = 0;
for( int n = 0; n < BUTTON_TYPE_NUM; n ++ ){
m_abShow[n] = false;
m_aIndex[n] = -1;
}
try{
Image img = Image.createImage("/demo/button.png");
m_ButtonSp = new Sprite(img, width, height);
m_ButtonSp.defineReferencePixel(width/2, height/2);
}
catch (Exception ex){}
m_aIndex[BUTTON_START] = startIndex;
m_aIndex[BUTTON_RESET] = resetIndex;
m_aIndex[BUTTON_CONTINUE] = continueIndex;
m_aIndex[BUTTON_HELP] = helpIndex;
m_aIndex[BUTTON_SAVE] = saveIndex;
m_aIndex[BUTTON_EXIT] = exitIndex;
}
//設(shè)置各種按鈕的顯示標(biāo)志
public void setShow( boolean bStart, boolean bReset, boolean bContinue,
boolean bHelp, boolean bSave, boolean bExit ){
m_abShow[BUTTON_START] = bStart;
m_abShow[BUTTON_RESET] = bReset;
m_abShow[BUTTON_CONTINUE] = bContinue;
m_abShow[BUTTON_HELP] = bHelp;
m_abShow[BUTTON_SAVE] = bSave;
m_abShow[BUTTON_EXIT] = bExit;
m_nShowNum = 0;
m_nFocusType = -1;
for( int n = 0; n < BUTTON_TYPE_NUM; n ++ ){
if( m_abShow[n] && m_aIndex[n] >= 0 ){
//設(shè)置第一個(gè)顯示的按鈕為當(dāng)前高亮的按鈕
if( m_nFocusType < 0 )
m_nFocusType = n;
m_nShowNum++;
}
}
}
//處理按鍵的輸入,返回選擇的按鍵類型
public int Input(int keyStates){
if( ( keyStates & GameCanvas.UP_PRESSED ) != 0 ){
int temp = m_nFocusType - 1;
while( temp >= 0 ){
if( m_abShow[temp] && m_aIndex[temp] >= 0 ){
m_nFocusType = temp;
break;
}
temp --;
}
}
if( ( keyStates & GameCanvas.DOWN_PRESSED ) != 0 ){
int temp = m_nFocusType + 1;
while( temp < BUTTON_TYPE_NUM ){
if( m_abShow[temp] && m_aIndex[temp] >= 0 ){
m_nFocusType = temp;
break;
}
temp ++;
}
}
if( ( keyStates & GameCanvas.FIRE_PRESSED ) != 0 ){
return m_nFocusType;
}
return -1;
}
//顯示按鈕
public void Paint(Graphics g, int scrWidth, int scrHeight){
if( m_ButtonSp == null )
return;
int h = m_ButtonSp.getHeight() + 2;
int x = 15;
int y = scrHeight - h * m_nShowNum - 15;
for( int n = 0; n < BUTTON_TYPE_NUM; n ++ )
{
if( m_abShow[n] && m_aIndex[n] >= 0 )
{//如果需要顯示按鈕,且該類型按鈕的圖像存在,則顯示
if( m_nFocusType == n )
{//顯示高亮的按鈕
m_ButtonSp.setFrame(m_aIndex[n]);
m_ButtonSp.setPosition(x, y);
m_ButtonSp.paint(g);
}
else
{//顯示灰色按鈕
int half = m_ButtonSp.getFrameSequenceLength() / 2;
m_ButtonSp.setFrame(m_aIndex[n] + half);
m_ButtonSp.setPosition(x, y);
m_ButtonSp.paint(g);
}
y = y + h;
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -