📄 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)
{
System.out.println("写入数据失败");
}
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 + -