📄 midlet7.java
字号:
package prj8_1;
import java.util.Date;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MIDlet7 extends MIDlet implements CommandListener{
private TextBox tbx = new TextBox("输入一个数字","",10,TextField.ANY);
private Display dis;
private Command cmd = new Command("打印平方",Command.SCREEN,1);
public MIDlet7() {
// TODO Auto-generated constructor stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
dis = Display.getDisplay(this);
dis.setCurrent(tbx);
tbx.addCommand(cmd);
tbx.setCommandListener(this);
Image img = Image.createImage("/file");
}
public void commandAction(Command c,Displayable d){
String str = tbx.getString();
try{
this.print(str);
}catch(CustomException ce){
System.out.println(ce.getMessage());
System.out.println(ce.getDate());
}
}
public void print(String str) throws CustomException{//表示这个函数可能要抛出异常给客户端
try{
int intValue = Integer.parseInt(str);
int result = intValue * intValue;
System.out.println("平方是:" + result);
}
catch(Exception ex){
CustomException ce = new CustomException("格式错误",new Date());
throw ce; //将ce抛给客户端
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
}
class CustomException extends Exception{//自定义异常
private Date date;
public CustomException(String message,Date date){
super(message);
this.date = date;
}
public Date getDate(){
return this.date;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -