?? 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 + -