?? gamename.java
字號:
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class GameName extends Form implements Runnable,CommandListener
{
//輸入名字畫面,繼承了FOMR類,并實現了Runnable和CommandListener接口
private TextField t;
static String name;
public GameName()
{
super("");
flag = true;
isInput = false;
}
//按鍵事件,得到玩家名字,或者調用導航器NAVIGATOR回到主畫面
public void commandAction(Command c,Displayable d)
{
String cmd = c.getLabel();
if(cmd.equals("確定"))
{
name = t.getString();
isInput = true;
}
else if(cmd.equals("返回"))
{
flag= false;
GameMenu.current = GameMenu.MY_RMS;
GameMenu.show();
}
}
boolean flag;
boolean isInput;
public void start()
{
Thread t = new Thread(this);
t.start();
}
public void run()
{
//加入輸入框TEXT FIELD和2個按鈕
t=new TextField("請輸入你的名字","無名氏",5,TextField.ANY);
append(t);
append("您的得分是"+": "+MyGame.score);
addCommand(new Command("確定",Command.OK,0));
addCommand(new Command("返回",Command.BACK,0));
setCommandListener(this);
//循環等待玩家輸入名字
while(flag)
{
//判斷是否接受名字,接受后,把資料輸入到數據庫中
if(isInput)
{
//打開數據庫
try
{
GameMIDlet.rs = RecordStore.openRecordStore(GameMIDlet.RecordName,false);
}
catch (Exception e)
{
System.out.println("打開數據庫失敗");
}
GameRMS rd = new GameRMS(name,MyGame.score);
byte[] current = rd.encode();
//寫入數據
try
{
GameMIDlet.rs.setRecord(11,current,0,current.length);
}
catch (Exception e)
{
}
GameRMS prerd = new GameRMS();
byte[] prebyte = null;
GameRMS nextrd = new GameRMS();
byte[] nextbyte = null;
byte[] temp = null;
//用冒泡法比較數據大小并排序
for(int i = 1;i<=10;i++)
{
for(int k=i+1;k<=11;k++)
{
try
{
prebyte = GameMIDlet.rs.getRecord(i);
nextbyte = GameMIDlet.rs.getRecord(k);
}
catch (Exception e)
{
System.out.println("讀取數據庫失敗");
}
prerd.decode(prebyte);
nextrd.decode(nextbyte);
if(nextrd.score>prerd.score)
{
temp = prebyte;
prebyte = nextbyte;
nextbyte = temp;
try
{
GameMIDlet.rs.setRecord(i,prebyte,0,prebyte.length);
GameMIDlet.rs.setRecord(k,nextbyte,0,nextbyte.length);
}
catch (Exception e)
{
System.out.println("寫入數據失敗");
}
}
}
}
//關閉數據庫
try
{
GameMIDlet.rs.closeRecordStore();
}catch(Exception e)
{
System.out.println("關閉數據庫失敗");
}
//調用導航器NAVIGATOR返回主畫面
GameMenu.current = GameMenu.MY_MENU;
GameMenu.show();
break;
}
try
{
Thread.sleep(100);
} catch (Exception e) {}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -