?? options.java
字號:
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
public class Options implements CommandListener, ItemStateListener
{
private FIRMIDlet midlet;
private Form form;
private Command CMD_OK;
private Command CMD_CANCEL;
private Gauge gaugeSize;
private ChoiceGroup choiceFirst;
private ChoiceGroup choiceDegree;
private int boardSize;
private boolean isComputerFirst;
private int degree;
public Options(FIRMIDlet m)
{
boardSize = 15;
isComputerFirst = true;
degree = 1;
midlet = m;
loadOptions();
form = new Form("游戲設置");
gaugeSize = new Gauge("棋盤大小: " + boardSize + " X " + boardSize, true, 10, boardSize - 10);
form.append(gaugeSize);
choiceFirst = new ChoiceGroup(null, 2);
choiceFirst.append("對方先行", null);
choiceFirst.setSelectedIndex(0, isComputerFirst);
form.append(choiceFirst);
choiceDegree = new ChoiceGroup("電腦智能級別:", 1);
choiceDegree.append("拜師學藝", null);
choiceDegree.append("初出茅廬", null);
choiceDegree.append("闖蕩江湖", null);
choiceDegree.append("誰與爭鋒", null);
choiceDegree.setSelectedIndex(degree-1 , true);
form.append(choiceDegree);
form.setItemStateListener(this);
CMD_OK = new Command("確定", 4, 2);
CMD_CANCEL = new Command("取消", 3, 1);
form.addCommand(CMD_OK);
form.addCommand(CMD_CANCEL);
form.setCommandListener(this);
}
public int getBoardSize()
{
return boardSize;
}
public boolean isComputerFirst()
{
return isComputerFirst;
}
public int getDegree()
{
return degree;
}
public Form getForm()
{
return form;
}
public void itemStateChanged(Item item)
{
if(item == gaugeSize)
{
int bs = gaugeSize.getValue() + 10;
gaugeSize.setLabel("棋盤大小: " + bs + " X " + bs);
}
}
public void commandAction(Command c, Displayable s)
{
if(c == CMD_OK)
{
boardSize = gaugeSize.getValue() + 10;
if(boardSize > 20)
boardSize = 20;
if(boardSize < 10)
boardSize = 10;
isComputerFirst = choiceFirst.isSelected(0);
degree = choiceDegree.getSelectedIndex() + 1;
saveOptions();
midlet.comeBack();
} else
if(c == CMD_CANCEL)
midlet.comeBack();
}
private void loadOptions()
{
try
{
RecordStore rs = RecordStore.openRecordStore("Options", false);
if(rs.getNumRecords() > 0)
{
byte bs[] = rs.getRecord(1);
if(bs.length >= 3)
{
boardSize = bs[0];
if(boardSize < 10)
boardSize = 10;
if(boardSize > 20)
boardSize = 20;
isComputerFirst = bs[1] == 1;
degree = bs[2];
if(degree < 1)
degree = 1;
if(degree > 4)
degree = 4;
}
}
rs.closeRecordStore();
}
catch(RecordStoreException e) { }
}
private void saveOptions()
{
try
{
RecordStore rs = RecordStore.openRecordStore("Options", true);
byte bs[] = new byte[3];
bs[0] = (byte)boardSize;
bs[1] = (byte)(isComputerFirst ? 1 : 0);
bs[2] = (byte)degree;
if(rs.getNumRecords() > 0)
rs.setRecord(1, bs, 0, 3);
else
rs.addRecord(bs, 0, 3);
rs.closeRecordStore();
}
catch(RecordStoreException e) { }
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -