📄 test.java
字号:
//创作者:杨明
//email:strive4all@126.com
package com.strive4all;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public final class Test extends MIDlet implements CommandListener {
/** The number of characters in numeric text field. */
private static final int NUM_SIZE = 20;
private final Command exitCmd = new Command("退出", Command.EXIT, 2);
private final Command startCmd = new Command("开始", Command.SCREEN, 1);
/** A text field to keep the first argument. */
private final TextField originalNum = new TextField(null, "", NUM_SIZE,
TextField.DECIMAL);
/** A text field to keep the second argument. */
private final TextField result = new TextField("结果", "", NUM_SIZE,
TextField.UNEDITABLE);
/** A choice group with available operations. */
private final ChoiceGroup choice = new ChoiceGroup("", ChoiceGroup.POPUP,
new String[] { "十进制->二进制", "二进制->十进制", }, null);
private final Alert alert = new Alert("Error", "", null, AlertType.ERROR);
/** Indicates if the application is initialized. */
private boolean isInitialized = false;
protected void startApp() {
if (isInitialized) {
return;
}
Form mainForm = new Form("进制转换(杨明)");
mainForm.append(originalNum);
mainForm.append(choice);
mainForm.append(result);
mainForm.addCommand(exitCmd);
mainForm.addCommand(startCmd);
mainForm.setCommandListener(this);
Display.getDisplay(this).setCurrent(mainForm);
alert.addCommand(new Command("返回", Command.SCREEN, 1));
isInitialized = true;
}
protected void destroyApp(boolean unconditional) {
}
protected void pauseApp() {
}
public void commandAction(Command c, Displayable d) {
if (c == exitCmd) {
destroyApp(false);
notifyDestroyed();
return;
}
int flag=0;
int n1=0;
try {
n1 = getNumber(originalNum, "First");
switch (choice.getSelectedIndex()) {
case 0: flag=10;
break;
case 1: flag=2;
break;
default:
}
} catch (NumberFormatException e) {
return;
} catch (ArithmeticException e) {
alert.setString("Divide by zero.");
Display.getDisplay(this).setCurrent(alert);
return;
}
String res_str=Integer.toString(n1);
if (res_str.length() > result.getMaxSize()) {
result.setMaxSize(res_str.length());
}
if(flag==10){
res_str=String.valueOf(Integer.toBinaryString(Integer.parseInt(res_str)));
}
if(flag==2){
res_str=String.valueOf(Integer.parseInt(res_str,2));
}
result.setString(res_str);
}
private int getNumber(TextField t, String type)
throws NumberFormatException {
String s = t.getString();
if (s.length() == 0) {
alert.setString("No " + type + " Argument");
Display.getDisplay(this).setCurrent(alert);
throw new NumberFormatException();
}
int n;
try {
n = Integer.parseInt(s);
} catch (NumberFormatException e) {
alert.setString(type + " argument is out of range.");
Display.getDisplay(this).setCurrent(alert);
throw e;
}
return n;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -