源代碼如下
// CalcMIDlet.Java
package my.calc;
import Javax.microedition.midlet.*;
import Javax.microedition.lcdui.*;
public class CalcMIDlet extends MIDlet {
private static CalcMIDlet instance;
private CalcCanvas calc = new CalcCanvas();
/** Constructor */
public CalcMIDlet() {
instance = this;
}
/** Main method */
public void startApp() {
Display.getDisplay(this).setCurrent(calc);
}
/** Handle pausing the MIDlet */
public void pauseApp() {
}
/** Handle destroying the MIDlet */
public void destroyApp(boolean unconditional) {
}
/** Quit the MIDlet */
public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
}
// CalcCanvas.Java
package my.calc;
import com.nokia.mid.ui.FullCanvas;
import Javax.microedition.lcdui.*;
import Java.io.*;
/**
* 計算器界面
* <p>Title: 基於Nokia S60的計算器</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* email:[email protected]
* @version 1.0
*/
public class CalcCanvas extends FullCanvas{
/**退出按鈕*/
private Command cmdExit;
/**標題*/
private String title = "Calculator";
/**字體*/
private Font f;
/**顯示的數字*/
private long result = 0;
/**焦點編號*/
private int num = 0;
/**焦點坐標*/
private int focusX = 5;
private int focusY = 52;
/**按鈕寬度間隔*/
private int intervalWidth = 2;
private int intervalHeight = 2;
/**圖片起點坐標*/
private int x = 10;
private int y = 70;
/**按鈕尺寸*/
private int buttonWidth = 37;
private int buttonHeight = 28;
/**圖片文件*/
private Image image = null;
/**存儲控件代表的內容*/
private char[] info = {'7','8','9','/',
'4','5','6','*',
'1','2','3','-',
'0','?','=','+'
};
/**運算中的第一個數字*/
private long firstNum = 0;
/**運算中的第二個數字*/
private long secondNum = 0;
/**是否輸入第一個數字,true代表已經輸入,false代表未輸入*/
private boolean flag = false;
/**存儲按下的運算按鈕的下標*/
private int func = 0;
/**默認構造方法*/
public CalcCanvas() {
try{
image = Image.createImage("/calc.png");
//System.out.println(x);
}catch(IOException e){
//ignore
System.out.println(e);
}
}
/**
* 繪制
* @param g 圖形對象
*/
public void paint(Graphics g) {
/**清除當前顯示*/
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
//恢復系統顏色
g.setColor(0,0,0);
f = g.getFont();
//繪制標題
g.drawString(title,getWidth() / 2,f.getHeight() / 2,Graphics.TOP|Graphics.HCENTER);
//繪制文本框
g.drawRect(3,24,172,20);
//繪制數字
String temp = Long.toString(result);
g.drawString(temp,getWidth() - 3,30,Graphics.TOP|Graphics.RIGHT);
//繪制面板
g.drawImage(image,2,50,Graphics.TOP|Graphics.LEFT);
//繪制焦點
drawFocus(g,num);
}
/**
* 事件處理
* @param keyCode 按鈕值
*/
public void keyPressed(int keyCode){
//System.out.println(keyCode);
//向左移動,如果移動到右邊界,則移動到下一行
if(keyCode == KEY_LEFT_ARROW){
//判斷是否到達最後一個
if(num == 0){
num = 15;
}else{
num--;
}
}
//向右移動,如果移動到左邊界,則移動到上一行末尾
if(keyCode == KEY_RIGHT_ARROW){
//判斷是否到達第一個
if(num == 15){
num = 0;
}else{
num++;
}
}
//向上移動,如果移動到上邊界,則移動到下邊界對應位置
if(keyCode == KEY_UP_ARROW){
//判斷是否移動到上邊界,如果是則移動到下邊界的對應位置
if(num - 4 < 0){
num = 15 + num - 3;
}else{
num = num - 4;
}
}
//向下移動,如果移動到下邊界,則移動到上邊界對應位置
if(keyCode == KEY_DOWN_ARROW){
//判斷是否達到下邊界,如果是則移動到上邊界的對應位置
if(num + 4 > 15){
num = num + 3 - 15;
}else{
num = num + 4;
}
}
//確定按鈕
if(keyCode == -5){
//根據選擇按鈕的不同進行操作
action();
}
//System.out.println("num : " + num);
//繪圖
repaint();
}
/**
* 繪制焦點矩形
* @param g 圖形對象
* @param num 焦點編號
*/
private void drawFocus(Graphics g,int num){
//設置繪制顏色為紅色
g.setColor(255,0,0);
//設置焦點的X和Y坐標
setXandYByNum(num);
//繪制選擇框
g.drawRect(focusX,focusY,buttonWidth,buttonHeight);
}
/**
* 根據焦點編號,設置焦點的X和Y坐標
* @param num 焦點編號
*/
private void setXandYByNum(int num){
focusX = 5 + (num % 4) * (buttonWidth + intervalWidth);
focusY = 52 + (num / 4) * (buttonHeight + intervalHeight);
}
/**
* 在用戶按下確定按鈕以後,根據選擇按鈕的不同進行操作
*/
private void action(){
//判斷焦點按鈕是否為數字
if((num >=0 && num < 3) || (num > 3 && num < 7) || (num > 7 && num < 11) || (num == 12)){
//如果是數字,則將原數字乘以10,再加上現在的數字
result = result * 10 + (info[num] - '0');
}
//如果按下的是負號,則求當前數字的負數
if(num == 13){
result = -result;
}
//判斷第一個數字是否輸入
if(flag == true){
secondNum = result;
//處理功能按鈕
//按下=按鈕,或者按下運算按鈕時,進行運算
if(num == 14 || (num % 4 == 3)){
//根據運算按鈕進行運算
if(func == 3){
//除法運算
result = firstNum / secondNum;
}else if(func == 7){
//乘法運算
result = firstNum * secondNum;
}else if(func == 11){
//減法運算
result = firstNum - secondNum;
}else if(func == 15){
//加法運算
result = firstNum + secondNum;
}
if(num == 14){
//將運算結果賦給第一個數字
firstNum = result;
//將標志修改為false
flag = false;
}else{
//記錄運算按鈕
func = num;
//將運算結果賦給第一個數字
firstNum = result;
//將顯示清零
result = 0;
}
}
}else{
//將當前值賦給第一個數字
firstNum = result;
//判斷是否按下運算按鈕,並存儲按下的運算按鈕
if(num % 4 == 3){
func = num;
//設置第一個數字已輸入,並將顯示清零
flag = true;
result = 0;
}
}
}
}