⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setting.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!..
💻 JAVA
字号:
/*
 *设置
 *作者:肖昶
 *
*/
package fivegame;

import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class Setting extends Form implements CommandListener {

	public static Setting instance = null;// 本类实例
	private static RecordStore rs = null;// 记录
	private static Command saveExit = null;// 返回保存命令
	private static Command clearWL = null;// 战绩清零命令

	// 构造函数
	Setting() {
		super("设置");
		setCommandListener(this);
		saveExit = new Command("返回保存", 3, 1);
		clearWL = new Command("战绩清零", 1, 2);
		addCommand(saveExit);
		addCommand(clearWL);
		TextField userName = new TextField("玩家:", "", 8, 0);
		userName.setLayout(515);
		StringItem win = new StringItem("胜 : ", "", 1);
		StringItem lost = new StringItem("负 : ", "", 2);
		openRecord();
		userName.setString(readUserName(rs));// 根据记录读取并显示玩家名
		short winAndFail[] = new short[2];
		readWLData(rs, winAndFail);// 读取玩家战绩信息
		// 显示战绩信息
		win.setText(String.valueOf(winAndFail[0]));
		lost.setText(String.valueOf(winAndFail[1]));
		append(userName);
		append(win);
		append(lost);
	}

	// 命令动作响应, CommandListener接口必须实现的方法
	public void commandAction(Command c, Displayable d) {
		if (c == clearWL) {// 清除玩家战绩信息
			writeWLData(rs, false, true, true);
			((StringItem) get(1)).setText(String.valueOf(0));
			((StringItem) get(2)).setText(String.valueOf(0));
		} else if (c == saveExit) {// 保存修改
			if (((TextField) get(0)).getString() != "") {// 名字改动了并且名字不为空
				writeUserName(rs, ((TextField) get(0)).getString(), false);// 修改
			}
			try {
				rs.closeRecordStore();// 关闭记录
				rs = null;
			} catch (RecordStoreException e) {
				e.printStackTrace();
			}
			instance = null;
			FiveGame.display.setCurrent(MainMenu.getInstance());
		}
	}

	// 从记录读取玩家名
	public static final String readUserName(RecordStore rs) {
		byte tempResult[] = (byte[]) null;
		String result = null;
		try {
			tempResult = rs.getRecord(1);
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		ByteArrayInputStream bis = new ByteArrayInputStream(tempResult);
		DataInputStream dis = new DataInputStream(bis);
		try {
			result = dis.readUTF();
			dis.close();
			bis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	// 从记录读取玩家战绩信息
	public static final void readWLData(RecordStore rs, short wfs[]) {
		byte result[] = (byte[]) null;
		try {
			result = rs.getRecord(2);
			wfs[0] = bytesToshort(result);
			result = rs.getRecord(3);
			wfs[1] = bytesToshort(result);
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
	}

	// 战绩信息写入操作
	public static final void writeWLData(RecordStore rs, boolean isFirst,
			boolean isClear, boolean isWriteWin) {
		short win = 0;
		short lost = 0;
		byte result[] = (byte[]) null;
		if (isFirst) {// 第一次写入战绩信息
			result = new byte[2];
			shortToBytes(win, result);
			shortToBytes(lost, result);
			try {
				rs.addRecord(result, 0, result.length);
				rs.addRecord(result, 0, result.length);
			} catch (RecordStoreException e) {
				e.printStackTrace();
			}
			return;
		}
		try {
			if (!isClear) {// 更新战绩信息
				int n = (isWriteWin == true ? 2 : 3);
				result = rs.getRecord(n);
				short temp = bytesToshort(result);
				temp += 1;
				shortToBytes(temp, result);
				rs.setRecord(n, result, 0, result.length);
			} else {// 清除战绩信息
				result = new byte[2];
				shortToBytes((short) 0, result);
				rs.setRecord(2, result, 0, result.length);
				rs.setRecord(3, result, 0, result.length);
			}
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
	}

	// 写入玩家名称
	public static final void writeUserName(RecordStore rs, String newName,
			boolean isFirst) {
		byte baUserName[] = (byte[]) null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(bos);
		try {
			dos.writeUTF(newName);
			baUserName = bos.toByteArray();
			dos.close();
			bos.close();
			if (isFirst)
				rs.addRecord(baUserName, 0, baUserName.length);
			else
				rs.setRecord(1, baUserName, 0, baUserName.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// ShortToBytes转换
	private static final void shortToBytes(short res, byte des[]) {
		des[0] = (byte) (0xff & res >> 8);
		des[1] = (byte) (0xff & res);
	}

	// bytesToshort转换
	private static final short bytesToshort(byte res[]) {
		short result = 0;
		result = (short) (res[0] & 0xff);
		result = (short) ((result << 8 & 0xff00) + (res[1] & 0xff));
		return result;
	}

	// 打开用户信息记录
	private static void openRecord() {
		if (rs == null) {
			try {// 打开用户信息记录
				rs = RecordStore.openRecordStore("UserInfo", true, 0, false);
			} catch (RecordStoreException e) {
				e.printStackTrace();
			}
		}
	}

	// 返回本类的实例
	public static final Displayable getInstance() {
		if (instance == null)
			instance = new Setting();
		openRecord();
		return instance;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -