求J2ME 手机程序代码
本人需求一个J2ME的手机计算器程序,要求:需要能在手机上运行```是MIDlet类的程序```与JAVA不同````还要手机屏幕显示如图中的图框,框中有数字1-9,我只...
本人需求一个J2ME 的手机计算器程序,要求:需要能在手机上运行```是MIDlet 类的程序```与JAVA 不同````还要手机屏幕显示如图中的图框,框中有数字1-9,我只要这个计算器能实现简单的加减乘除````其他的什么都不需要```液晶显示不要```高手注意, 只要代码```
展开
2个回答
展开全部
总共2个类
package text2;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MIDlet1 extends MIDlet {
static MIDlet1 instance;
Displayable1 displayable = new Displayable1();
public MIDlet1() {
instance = this;
}
public void startApp() {
Display.getDisplay(this).setCurrent(displayable);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
}
package text2;
import javax.microedition.lcdui.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Displayable1 extends Canvas {
public Displayable1() {
setFullScreenMode(true);
}
String[] numStr = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#"
};
String Str = "0";
int tem;
int keynum;
boolean clearFlag = true;
int oper; //第一次操作运算变量
int oper1 = 0; // = 后 两值 操作变量
protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, 176, 208);
g.setColor(0);
g.drawRect(5, 5, 160, 16); // 显示条
g.drawString(Str, 160, 10, 24); // 添加输入数字
g.drawArc(80, 25, 15, 15, 0, 360);
g.drawString("+", 88, 28, 17);
g.drawArc(60, 45, 15, 15, 0, 360);
g.drawString("*", 68, 48, 17);
g.drawRect(80, 45, 15, 15);
g.drawString("=", 88, 48, 17);
g.drawArc(100, 45, 15, 15, 0, 360);
g.drawString("/", 108, 48, 17);
g.drawArc(80, 65, 15, 15, 0, 360);
g.drawString("-", 88, 68, 17);
int index = 0;
for (int j = 0; j < 4; j++) { //数字键
for (int i = 0; i < 3; i++) {
g.drawRect(35 + 35 * i, 85 + 20 * j, 30, 15);
g.drawString(numStr[index++], 50 + 35 * i, 88 + 20 * j, 17);
}
}
}
public void update() {
}
public void input() {
switch (keynum) {
case -2: // -
oper = 2;
clearFlag = true;
calculate();
break;
case -1: // +
oper = 1;
clearFlag = true;
calculate();
break;
case -3: // *
oper = 3;
clearFlag = true;
calculate();
break;
case -4: // /
oper = 4;
clearFlag = true;
calculate();
break;
case -5: // =
oper = 5;
clearFlag = true;
calculate();
break;
case 42:
Str = "0";
break;
case 35:
Str = "0";
break;
}
if (keynum >= 48 && keynum <= 57) {
if (clearFlag) {
Str = ""; // 清空值
if (keynum == 48) {
clearFlag = true;
}
else {
clearFlag = false;
}
}
Str += getKeyName(keynum);
}
repaint();
}
public void keyPressed(int key) {
keynum = key;
input();
}
public void calculate() { //运算
try {
int num = Integer.parseInt(Str);
switch (oper) { // 给相应运算付值 oper1 是操作运算变量
case 1:
oper1 = 1;
tem = num; //保存第一个数
break;
case 2:
oper1 = 2;
tem = num;
break;
case 3:
oper1 = 3;
tem = num;
break;
case 4:
oper1 = 4;
tem = num;
break;
case 5 :
switch (oper1) { //判断运算
case 1:
tem = tem + num;
break;
case 2:
tem = tem - num;
break;
case 3:
tem = tem * num;
break;
case 4:
if (num != 0) {
tem = tem / num;
}
break;
}
}
}
catch (Exception e) {
tem = 0;
e.printStackTrace();
}
Str = String.valueOf(tem);
}
}
package text2;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MIDlet1 extends MIDlet {
static MIDlet1 instance;
Displayable1 displayable = new Displayable1();
public MIDlet1() {
instance = this;
}
public void startApp() {
Display.getDisplay(this).setCurrent(displayable);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
}
package text2;
import javax.microedition.lcdui.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Displayable1 extends Canvas {
public Displayable1() {
setFullScreenMode(true);
}
String[] numStr = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#"
};
String Str = "0";
int tem;
int keynum;
boolean clearFlag = true;
int oper; //第一次操作运算变量
int oper1 = 0; // = 后 两值 操作变量
protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, 176, 208);
g.setColor(0);
g.drawRect(5, 5, 160, 16); // 显示条
g.drawString(Str, 160, 10, 24); // 添加输入数字
g.drawArc(80, 25, 15, 15, 0, 360);
g.drawString("+", 88, 28, 17);
g.drawArc(60, 45, 15, 15, 0, 360);
g.drawString("*", 68, 48, 17);
g.drawRect(80, 45, 15, 15);
g.drawString("=", 88, 48, 17);
g.drawArc(100, 45, 15, 15, 0, 360);
g.drawString("/", 108, 48, 17);
g.drawArc(80, 65, 15, 15, 0, 360);
g.drawString("-", 88, 68, 17);
int index = 0;
for (int j = 0; j < 4; j++) { //数字键
for (int i = 0; i < 3; i++) {
g.drawRect(35 + 35 * i, 85 + 20 * j, 30, 15);
g.drawString(numStr[index++], 50 + 35 * i, 88 + 20 * j, 17);
}
}
}
public void update() {
}
public void input() {
switch (keynum) {
case -2: // -
oper = 2;
clearFlag = true;
calculate();
break;
case -1: // +
oper = 1;
clearFlag = true;
calculate();
break;
case -3: // *
oper = 3;
clearFlag = true;
calculate();
break;
case -4: // /
oper = 4;
clearFlag = true;
calculate();
break;
case -5: // =
oper = 5;
clearFlag = true;
calculate();
break;
case 42:
Str = "0";
break;
case 35:
Str = "0";
break;
}
if (keynum >= 48 && keynum <= 57) {
if (clearFlag) {
Str = ""; // 清空值
if (keynum == 48) {
clearFlag = true;
}
else {
clearFlag = false;
}
}
Str += getKeyName(keynum);
}
repaint();
}
public void keyPressed(int key) {
keynum = key;
input();
}
public void calculate() { //运算
try {
int num = Integer.parseInt(Str);
switch (oper) { // 给相应运算付值 oper1 是操作运算变量
case 1:
oper1 = 1;
tem = num; //保存第一个数
break;
case 2:
oper1 = 2;
tem = num;
break;
case 3:
oper1 = 3;
tem = num;
break;
case 4:
oper1 = 4;
tem = num;
break;
case 5 :
switch (oper1) { //判断运算
case 1:
tem = tem + num;
break;
case 2:
tem = tem - num;
break;
case 3:
tem = tem * num;
break;
case 4:
if (num != 0) {
tem = tem / num;
}
break;
}
}
}
catch (Exception e) {
tem = 0;
e.printStackTrace();
}
Str = String.valueOf(tem);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询