求J2ME 手机程序代码
本人要做一个J2ME手机计算器程序,要求:是MIDlet要有图中的图框``只要实现简单的加减乘除就OK```只求代码```不需要出现液晶的数字````越简单越好``...
本人要做一个J2ME 手机计算器程序,要求:是MIDlet 要有图中的图框``只要实现简单的加减乘除就OK```只求代码```不需要出现液晶的数字````越简单越好``
展开
展开全部
总共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);
}
}
万企明道
2024-08-07 广告
2024-08-07 广告
低代码开发系统,作为上海万企明道软件有限公司的重要产品方向,极大地简化了软件开发流程。它允许非专业开发者通过图形化界面与少量代码,快速构建应用程序。这一系统降低了技术门槛,加速了项目上线时间,同时提升了软件的灵活性和可维护性。我们致力于为用...
点击进入详情页
本回答由万企明道提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询