📄 notepad.java
字号:
/*
* 分页显示一篇文章
*/
package notepad;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class NotePad extends MIDlet implements CommandListener {
private Command exitCommand,nextCommand,beginCommand;
private TextBox tb;
private String str;
private int position=0;//记录当前分段的位置
public NotePad() {
exitCommand = new Command("退出", Command.EXIT, 1);
nextCommand = new Command("下一页", Command.SCREEN, 1);
beginCommand = new Command("重新阅读", Command.SCREEN, 1);
tb = new TextBox("NotePad Example", "Hello,World!", 500,TextField.ANY);
//txt文件必须使用ascii格式保存的,否则不能显示中文
str = loadText("/war3.txt");
firstText();
tb.addCommand(exitCommand);
tb.setCommandListener(this);
}
protected void firstText(){
// 分页显示文章
if(str.length()<=10){
tb.setString(str);
position = 0;
}
else{
tb.setString(str.substring(0,10));
position = 1;
tb.addCommand(nextCommand);
}
}
protected String loadText(String url){
String str = null;
InputStream is = this.getClass().getResourceAsStream(url);
try {
byte[] data = new byte[is.available()];
is.read(data);
is.close();
is=null;
str = new String(data);
} catch (IOException e) {
e.printStackTrace();
return "";
}
if(str != null){
return str;
}
else
return "";
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(tb);
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
try {
destroyApp(false);
} catch (MIDletStateChangeException exception) {
System.out.println("MIDletStateChangeException");
}
notifyDestroyed();
}
if (c == nextCommand){
//如果存在多页
if(str.substring(10*position).length()<10){
tb.setString(str.substring(10*position));
//消除“下一页”按钮
tb.removeCommand(nextCommand);
//显示“重新显示”按钮
tb.addCommand(beginCommand);
}
else{
tb.setString(str.substring(10*position,10*position+10));
position = position + 1;
}
}
if (c == beginCommand){
firstText();
tb.removeCommand(beginCommand);
tb.addCommand(nextCommand);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -