?? top.java
字號:
//導(dǎo)入相關(guān)的類
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.rms.*;
import java.io.*;
public class TOP extends Form implements CommandListener{
TextField name = null;//接受用戶名
TextField scorce = null;//接受用戶分?jǐn)?shù)
Command add = null;//添加命令
Command show = null;//顯示積分榜命令
Command back = null;//返回命令
RecordStore rs = null;//記錄倉庫
String userName[] = new String[5];//記錄用戶名
int userScorce[] = new int[5];//記錄用戶分?jǐn)?shù)
//構(gòu)造方法
public TOP()
{
super("scorce");//設(shè)置標(biāo)題
openRS("TOP-5");//打開紀(jì)錄倉庫
init();//初始化界面
}
//初始化界面
public void init()
{
deleteAll();//刪除界面以前的組件
name = new TextField("用戶名:","",15,TextField.ANY);
scorce = new TextField("分?jǐn)?shù):","",10,TextField.NUMERIC);
add = new Command("Add",Command.OK,1);
show = new Command("Top",Command.BACK,1);
append(name);
append(scorce);
addCommand(add);
addCommand(show);
setCommandListener(this);
}
//軟鍵事件處理
public void commandAction(Command c,Displayable d)
{
//添加記錄
if(c.equals(add))
{
//獲得用戶的用戶名和分?jǐn)?shù)
String name = this.name.getString();
int scorce = Integer.parseInt(this.scorce.getString());
if(isInsert(scorce))//判斷是否需要加入到記錄倉庫中
{
setRS(name,scorce);//將紀(jì)錄添加到紀(jì)錄倉庫中
}
this.name.setString("");
this.scorce.setString("");
}else if(c.equals(show))//顯示所有記錄
{
deleteAll();//刪除上一界面的組建
getAllRecord();//獲得記錄倉庫中的所有記錄
//將記錄倉庫中的信息顯示在當(dāng)前容器中
append(" 積分榜\n");
for(int i = 0;i < 5;i ++)
{
append((i+1)+" "+userName[i]+" "+userScorce[i]+'\n');
}
removeCommand(add);
removeCommand(show);
back = new Command("Back",Command.BACK,1);
addCommand(back);
}else if(c.equals(back))//返回輸入界面
{
removeCommand(back);
init();
}
}
//打開記錄倉庫
public void openRS(String s)
{
if(rs == null)
{
try
{
rs = RecordStore.openRecordStore(s, false);
}catch(Exception e)
{
try
{
rs = RecordStore.openRecordStore(s, true);//新建一個記錄倉庫
createRS();//創(chuàng)建初始記錄
}catch(Exception e1)
{
e1.printStackTrace();
}
}
}
}
//創(chuàng)建5條初始記錄
public void createRS()
{
if(rs != null)
{
for(int i = 0;i < 5;i ++)
{
addRecord("null",0);//向記錄倉庫中添加記錄
}
}
}
//添加記錄
public int addRecord(String name,int scorce)
{
if(rs != null)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();//創(chuàng)建數(shù)組輸出流
DataOutputStream dos = new DataOutputStream(bos);//數(shù)據(jù)輸出流
try
{
dos.writeUTF(name);//向流中寫入用戶名
dos.writeInt(scorce);//向流中寫入用戶成績
int temp =
rs.addRecord(bos.toByteArray(), 0, bos.toByteArray().length);//添加記錄
dos.close();//關(guān)閉數(shù)據(jù)流
bos.close();//關(guān)閉流
return temp;
}catch(Exception e)
{
e.printStackTrace();
}
}
return -1;
}
//獲得倉庫中所有記錄
public void getAllRecord()
{
if(rs != null)
{
for(int i = 1;i <= 5;i ++)
{
try
{
ByteArrayInputStream bis = new ByteArrayInputStream(rs.getRecord(i));//byte數(shù)組輸入流
DataInputStream dis = new DataInputStream(bis);//數(shù)據(jù)輸入流
userName[i-1] = dis.readUTF();//讀取用戶名
userScorce[i-1] = dis.readInt();//讀取用戶成績
dis.close();//關(guān)閉數(shù)據(jù)輸入流
bis.close();//關(guān)閉輸入流
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
//判斷新的分?jǐn)?shù)是否需要加入到記錄倉庫中
public boolean isInsert(int scorce)
{
if(rs != null)
{
try
{
//獲得記錄倉庫中最后一條記錄
ByteArrayInputStream bis = new ByteArrayInputStream(rs.getRecord(5));
DataInputStream dis = new DataInputStream(bis);
dis.readUTF();
int temp = dis.readInt();
//判斷當(dāng)前成績是否能添加到記錄倉庫
if(scorce > temp)
{
return true;
}
}catch(Exception e)
{
e.printStackTrace();
}
}
return false;
}
//把新的分?jǐn)?shù)添加到記錄倉庫中
public void setRS(String name,int scorce)
{
System.out.println(name+","+scorce);
int temp = 0;
String n = null;
getAllRecord();
for(int i = 0;i < 5;i ++)
{
//判斷分?jǐn)?shù)應(yīng)該插入到哪個位置
if(scorce > userScorce[i])
{
//交換用戶成績
temp = userScorce[i];
userScorce[i] = scorce;
scorce = temp;
//交換用戶名
n = userName[i];
userName[i] = name;
name = n;
try
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF(userName[i]);
dos.writeInt(userScorce[i]);
rs.setRecord(i + 1, bos.toByteArray(), 0, bos.toByteArray().length);//修改當(dāng)前位置記錄數(shù)據(jù)
dos.close();
bos.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
//關(guān)閉記錄倉庫
public void close()
{
if(rs != null)
{
try
{
rs.closeRecordStore();//關(guān)閉打開的記錄倉庫
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -