📄 countercanvas.java
字号:
import javax.microedition.lcdui.*;
//主程序
public class CounterCanvas extends Canvas {
// 按钮选中图片
Image igButton0;
// 按钮按下图片
Image igButton1;
// 屏显数字图片
Image igNumber;
// 错误提示图片
Image igs1;
Image igs2;
Image igs3;
// 界面背景图片(包括初始按键、标题和说明、退出按钮)
Image igui0;
// 显示框图片
Image igui1;
// 说明图片
Image igui2;
// 字串存储器(顺序为:第一个运算数、第一个运算符号、第二个运算数、计算结果、屏显)
String[] strMemorizer = new String[5];
// 主按键索引号,初始为20,显示为按钮非选中状态
int index = 20;
// 数字键名与对应的索引号数组
int[] indexArray = { 16, 12, 13, 14, 8, 9, 10, 4, 5, 6 };
String[] strNumber = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
// 运算符键索引号数组与对应的字符数组
int[] indexFh = { 19, 15, 11, 7 };
char[] fh = { '+', '-', '*', '/' };
Graphics g;
// 变量、刷屏标签、前一按键索引记忆器
int i, j, lable, n;
// 临时字串变量(记住:用后要清空)
String tmp = "";
// 获取上次按键索引号存储数组
int[] aheadKeycode = { 20, 20 };
// 构造方法中引入所需图片
public CounterCanvas() {
try {
igui0 = Image.createImage("/ui0.png");
igui1 = Image.createImage("/ui1.png");
igui2 = Image.createImage("/ui2.png");
igNumber = Image.createImage("/number.png");
igs1 = Image.createImage("/s1.png");
igs2 = Image.createImage("/s2.png");
igs3 = Image.createImage("/s3.png");
igButton0 = Image.createImage("/button.png");
igButton1 = Image.createImage("/button0.png");
} catch (Exception e) {
}
}
// 刷屏
protected void paint(Graphics g) {
// 画界面、底层背景
g.drawImage(igui0, 0, 0, Graphics.TOP | Graphics.LEFT);
// 画输出立体框(等同于屏显清空)
cls(g);
// 按键刷屏
switch (lable) {
// 按键选中刷屏
case 0:
for (i = 0; i < 20; i++) {
if (i == index) {
g.setClip(12 + i % 4 * 57, 84 + i / 4 * 35, 45, 31);
g.drawImage(igButton0, 12 + i % 4 * 57, 84 + i / 4 * 35 - i
* 31, Graphics.TOP | Graphics.LEFT);
g.setClip(0, 0, this.getWidth(), this.getHeight());
drawScreen(strMemorizer[4], g);
}
}
break;
// 按键确认刷屏
case 1:
for (i = 0; i < 20; i++) {
if (i == index) {
g.setClip(12 + i % 4 * 57, 84 + i / 4 * 35, 45, 31);
g.drawImage(igButton1, 12 + i % 4 * 57, 84 + i / 4 * 35 - i
* 31, Graphics.TOP | Graphics.LEFT);
g.setClip(0, 0, this.getWidth(), this.getHeight());
drawScreen(strMemorizer[4], g);
}
}
break;
case 2:
g.drawImage(igui2, 0, 0, Graphics.TOP | Graphics.LEFT);
break;
}
if (lable != 2) {
drawScreen(strMemorizer[4], g);
}
}
// 手机按键按下,计算器按键被选中(触发)
public void keyPressed(int keyCode) {
int action = this.getGameAction(keyCode);
// 手机键盘按键镜像
if (keyCode == 48) { // 0键
index = 16;
action = FIRE;
}
if (keyCode == 49) { // 1键
index = 12;
action = FIRE;
}
if (keyCode == 50) { // 2键
index = 13;
action = FIRE;
}
if (keyCode == 51) { // 3键
index = 14;
action = FIRE;
}
if (keyCode == 52) { // 4键
index = 8;
action = FIRE;
}
if (keyCode == 53) { // 5键
index = 9;
action = FIRE;
}
if (keyCode == 54) { // 6键
index = 10;
action = FIRE;
}
if (keyCode == 55) { // 7键
index = 4;
action = FIRE;
}
if (keyCode == 56) { // 8键
index = 5;
action = FIRE;
}
if (keyCode == 57) { // 9键
index = 6;
action = FIRE;
}
if (keyCode == -8) { // clear键改成镜像到撤消键和清零键
if (index == 1) {
index = 2;
} else if (index == 2) {
index = 17;
} else {
index = 1;
}
}
if (keyCode == 42) { // 用*键切换运算符号
if (index == 19) {
index = 15;
} else if (index == 15) {
index = 11;
} else if (index == 11) {
index = 7;
} else if (index == 7) {
index = 3;
} else {
index = 19;
}
}
// 按键触发
switch (action) {
case UP: // 上移选择框
if (index == 20) {
index = 9;
break;
}
if (index / 4 > 0) {
index -= 4;
}
break;
case DOWN: // 下移选择框
if (index == 20) {
index = 9;
break;
}
if (index / 4 < 4) {
index += 4;
}
break;
case LEFT: // 左移选择框
if (index == 20) {
index = 9;
break;
}
if (index % 4 > 0) {
index--;
}
break;
case RIGHT: // 右移选择框
if (index == 20) {
index = 9;
break;
}
if (index % 4 < 3) {
index++;
}
break;
case KEY_POUND: // 返回上一界面(用在从说明界面返回)
lable = 0;
break;
case FIRE:
// 计算器按下的前一键的索引号记忆器,在此初始化后传递给其它功能键用,也为按键切换做准备(否则会出现切换停顿)
n = anteriorIndex();
if (index == 20) {
index = 9;
break;
}
switch (index) {
case 0: // 向前消除键
backSpace();
break;
case 1: // 取消键
cancel();
break;
case 2: // 清零键
clsZero();
break;
case 3: // 等号键
amount();
break;
case 4: // 数字键7
case 5: // 数字键8
case 6: // 数字键9
case 8: // 数字键4
case 9: // 数字键5
case 10: // 数字键6
case 12: // 数字键1
case 13: // 数字键2
case 14: // 数字键3
case 16: // 数字键0
getNumber();
break;
case 7: // 除号键
case 11: // 乘号键
case 15: // 减号键
case 19: // 加号键
getOperator();
break;
case 17: // 取反键
sign();
break;
case 18: // 小数点未定义
break;
}
lable = 1;
repaint();
return;
}
lable = 0;
if (keyCode == -7) { // 右软键查看说明
lable = 2;
}
if (keyCode == -6) { // 左软键退出
CounterMidlet.quitApp();
}
repaint();
}
// 运算功能
public String operation(String s) {
tmp = "";
for (i = 0; i < s.length(); i++) {
if (s.charAt(i) == '+') {
tmp = String.valueOf(Integer.parseInt(s.substring(0, i))
+ Integer.parseInt(s.substring(i + 1, s.length())));
break;
}
if (s.charAt(i) == '-') {
if (i == 0) {
continue;
}
tmp = String.valueOf(Integer.parseInt(s.substring(0, i))
- Integer.parseInt(s.substring(i + 1, s.length())));
break;
}
if (s.charAt(i) == '*') {
tmp = String.valueOf(Integer.parseInt(s.substring(0, i))
* Integer.parseInt(s.substring(i + 1, s.length())));
break;
}
if (s.charAt(i) == '/') {
if (s.equals("0/0")) {
tmp = "函数无意义!";
return tmp;
}
if (s.endsWith("/0")) {
tmp = "零不能为除数!";
return tmp;
}
tmp = String.valueOf(Integer.parseInt(s.substring(0, i))
/ Integer.parseInt(s.substring(i + 1, s.length())));
break;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -