📄 formconf.java
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
public class FormConf extends Form implements CommandListener {
private Command okCommand = new Command("保存", Command.OK, 2);
private ChoiceGroup proxyCG = new ChoiceGroup("选择连接方式",
ChoiceGroup.EXCLUSIVE, new String[] { "cmwap", "cmnet" }, null);
public FormConf() {
super("连接方式");
super.addCommand(okCommand);
super.append(this.proxyCG);
if (ImageAlbum.getConnectionMethod()==null){
this.proxyCG.setSelectedIndex(0,true);
}else{
if (ImageAlbum.getConnectionMethod().equals("cmwap")){
this.proxyCG.setSelectedIndex(0,true);
}else{
this.proxyCG.setSelectedIndex(1,true);
}
}
}
public void commandAction(Command c, Displayable d) {
String proxy = null;
if (this.proxyCG.getSelectedIndex() == 0) {
proxy = "cmwap";
} else {
proxy = "cmnet";
}
if (c == this.okCommand) {
save(proxy);
ImageAlbum.setConnectionMethod(proxy);
ImageAlbum.setCurrentForm(ImageAlbum.getPreviousForm());
ImageAlbum.getDisplay().setCurrent(ImageAlbum.getPreviousForm());
}
}
/*
*
* 将连接方式存入RMS
*/
public static boolean save(String connectonMethod) {
RecordStore rStore = null;
ByteArrayOutputStream baos = null;
DataOutputStream dos = null;
try {
baos = new ByteArrayOutputStream();
dos = new DataOutputStream(baos);
dos.writeUTF(connectonMethod);
// 打开RecordStore,如果不存在,则创建一个
rStore = RecordStore.openRecordStore("CONNECTION_METHOD", true);
if (rStore.getNumRecords() == 0) {
rStore.addRecord(baos.toByteArray(), 0,
baos.toByteArray().length); // Add
} else {
rStore.setRecord(1, baos.toByteArray(), 0,
baos.toByteArray().length); // Add
}
} catch (Error oe) {
// oe.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (rStore != null) {
rStore.closeRecordStore();
rStore = null;
}
baos = null;
dos = null;
} catch (RecordStoreNotOpenException recordstorenotopenexception) {
recordstorenotopenexception.printStackTrace();
} catch (RecordStoreException recordstoreexception) {
recordstoreexception.printStackTrace();
}
}
return true;
}
/*
*
* 将连接方式从RMS里读出来
*/
public static String loadConnectionMethod() {
RecordStore rStore = null;
byte[] b = null;
ByteArrayInputStream bin = null;
DataInputStream din = null;
String connectionMethod = null;
try {
rStore = RecordStore.openRecordStore("CONNECTION_METHOD", true);
if (rStore.getNumRecords() == 0) {
return null;
} else {
b = rStore.getRecord(1);
bin = new ByteArrayInputStream(b);
din = new DataInputStream(bin);
connectionMethod = din.readUTF();
}
bin.reset();
din.close();
} catch (IOException e) {
e.printStackTrace();
} catch (RecordStoreException recordstoreexception) {
recordstoreexception.printStackTrace();
} finally {
try {
rStore.closeRecordStore();
rStore = null;
b = null;
din = null;
bin = null;
} catch (RecordStoreNotOpenException recordstorenotopenexception) {
recordstorenotopenexception.printStackTrace();
} catch (RecordStoreException recordstoreexception) {
recordstoreexception.printStackTrace();
}
}
return connectionMethod;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -