📄 note.java
字号:
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Note extends MIDlet implements CommandListener{
Display dis;
List list;
TextBox tb;
TextBox tb2;
Form f;
TextField tf;
Command cmdEXIT;
Command cmdADD;
Command cmdRENAME;
Command cmdEDIT;
Command cmdCANCEL;
Command cmdSAVE;
Command cmdBACK;
Command cmdBREAK;
Vector v;
Alert alert1;
Alert alert2;
Alert alert3;
static int index;
static int count = 0;
static Displayable d;
static boolean bl = true;
public Note() {
// TODO Auto-generated constructor stub
dis = Display.getDisplay(this);
list = new List("小小笔记本",Choice.IMPLICIT);
tb = new TextBox("","",256,TextField.ANY);
tb2 = new TextBox("","",256,TextField.ANY);
f = new Form("- 重命名 -");
tf = new TextField("请输入新文件名","",10,TextField.ANY);
cmdEXIT = new Command("退出",Command.EXIT,1);
cmdADD = new Command("新增",Command.SCREEN,1);
cmdRENAME = new Command("重命名",Command.SCREEN,1);
cmdEDIT = new Command("编辑",Command.SCREEN,1);
cmdCANCEL = new Command("删除",Command.CANCEL,1);
cmdSAVE = new Command("保存",Command.SCREEN,1);
cmdBACK = new Command("返回",Command.BACK,1);
cmdBREAK = new Command("退出",Command.EXIT,1);
v = new Vector(10);
alert1 = new Alert("","Sorry, no file can be Active!",null,AlertType.WARNING);
alert2 = new Alert("","保存成功!",null,AlertType.INFO);
alert3 = new Alert("","改动将不被保存!\n 是否仍要退出?",null,AlertType.WARNING);
alert3.setTimeout(Alert.FOREVER);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
// 添加按键群
public void addCommands(){
list.addCommand(cmdEXIT);
list.addCommand(cmdADD);
list.addCommand(cmdRENAME);
list.addCommand(cmdEDIT);
list.addCommand(cmdCANCEL);
tb.addCommand(cmdBACK);
tb.addCommand(cmdSAVE);
tb2.addCommand(cmdBACK);
tb2.addCommand(cmdSAVE);
f.removeCommand(cmdSAVE);
f.addCommand(cmdBACK);
f.addCommand(cmdEDIT);
alert3.addCommand(cmdSAVE);
alert3.addCommand(cmdBREAK);
}
// 添加
public void addNote(){
tb.setString("");
dis.setCurrent(tb);
}
// 编辑
public void editNote(){
bl = true;
if(list.size()!=0){
index = list.getSelectedIndex();
tb2.setTitle("- 编辑模式 -"+list.getString(index));
//System.out.println((String)(v.elementAt(index)));
tb2.setString((String)(v.elementAt(index)));
dis.setCurrent(tb2);
}else{
dis.setCurrent(alert1);
}
}
// 删除
public void cancelNote(){
if(list.size()!=0){
index = list.getSelectedIndex();
v.removeElementAt(index);
list.delete(index);
}else{
dis.setCurrent(alert1);
}
}
// 重命名
public void reName(){
f.removeCommand(cmdEDIT);
f.addCommand(cmdSAVE);
index = list.getSelectedIndex();
if(list.size()==0){
dis.setCurrent(alert1);
}else{
bl = true;
f.deleteAll();
f.append(tf);
tf.setString(list.getString(index));
dis.setCurrent(f);
}
}
// Start()
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
addCommands();
list.setCommandListener(this);
tb.setCommandListener(this);
tb2.setCommandListener(this);
f.setCommandListener(this);
alert3.setCommandListener(this);
dis.setCurrent(list);
}
// 监听
public void commandAction(Command cmd, Displayable disp) {
// TODO Auto-generated method stub
if(cmd==List.SELECT_COMMAND){
showNote();
}
if(disp==tb||disp==tb2||disp==f||disp==alert3){
if(cmd==cmdSAVE){
saveNote(disp);
}else if(cmd==cmdBACK){
if(bl==true){
dis.setCurrent(alert3,list);
}else
backToList();
}
if(cmd==cmdBREAK){
backToList();
}
}
if(cmd==cmdEXIT){
notifyDestroyed();
}
if(cmd==cmdADD){
addNote();
}
if(cmd==cmdEDIT){
editNote();
}
if(cmd==cmdCANCEL){
cancelNote();
}
if(cmd==cmdRENAME){
reName();
}
}
// 保存
public void saveNote(Displayable d){
bl = false;
if(d == alert3){
dis.setCurrent(alert2,list); // 保存并退出
if(list.size()==0)
d = tb;
else{
d = tb2;
list.set(index, tf.getString(), null);
}
}else{
dis.setCurrent(alert2,list); // 仅仅提示保存成功
}
if(d == tb){
list.append("文档"+count, null);
v.addElement(tb.getString());
count++;
}
if(d == tb2){
String s = tb2.getTitle();
list.set(list.getSelectedIndex(), s.substring("- 查看模式 -".length(), s.length()), null);
v.setElementAt(tb2.getString(), list.getSelectedIndex());
}
if(d == f){
list.set(index, tf.getString(), null);
}
}
// 返回
public void backToList(){
bl = true;
dis.setCurrent(list);
}
// 查看
public void showNote(){
f.deleteAll();
f.removeCommand(cmdSAVE);
f.removeCommand(cmdBACK);
f.addCommand(cmdEDIT);
f.addCommand(cmdBREAK);
f.setTitle("- 查看模式 -"+list.getString(list.getSelectedIndex()));
index = list.getSelectedIndex();
//System.out.println((String)(v.elementAt(index)));
f.append((String)(v.elementAt(index)));
dis.setCurrent(f);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -