J2me 入门路之---计数器

好 今天就先来练练j2me中间代的见表

做一个四则运算器。

首先新建一个Ticker 标题

首先是新建一个MIDlet

[align=left]package com.sa.calculate;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class CalMdilet extends MIDlet{

private StringBuffer sb;
private int keyPress ;
private CalCanvas can ;
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException {
Display d = Display.getDisplay(this);
can = new CalCanvas();
can.setTicker(new Ticker("calculate demo"));
d.setCurrent(can);
}
}

然后写一个Canvas吧:

package com.sa.calculate;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class CalCanvas extends Canvas {
String sb = new String();

protected void paint(Graphics g) {
// TODO Auto-generated method stub
g.setColor(255,255,255) ;
g.fillRect(0,0,getWidth(),getHeight()) ;
g.setColor(0,0,0) ;
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,
Font.SIZE_LARGE));

g.drawString(sb, 110, 120, Graphics.BASELINE| Graphics.HCENTER);
}
//覆盖按键响应方法
public void keyPressed(int keyCode) {


switch (keyCode) {
case Canvas.KEY_NUM0:
if (sb.length() == 0) {
break;
} else {
sb += "0";
// StringItem s = new StringItem("0",String.valueOf(num));
// f.append(String.valueOf(num));
System.out.println(sb);
repaint();
break;
}
case Canvas.KEY_NUM1:
sb += "1";
System.out.println(sb);
repaint();
break;
case Canvas.KEY_NUM2:
sb += "2";
System.out.println(sb);
repaint();
break;
case Canvas.KEY_NUM3:
sb += "3";
System.out.println(sb);
repaint();
break;
case Canvas.KEY_NUM4:
sb += "4";
System.out.println(sb);
repaint();
break;
case Canvas.KEY_NUM5:
sb += "5";
System.out.println(sb);
repaint();
break;
case Canvas.KEY_NUM6:
sb += "6";
System.out.println(sb);
repaint();
break;
case Canvas.KEY_NUM7:
sb += "7";
System.out.println(sb);
repaint();
break;
case Canvas.KEY_NUM8:
sb += "8";
System.out.println(sb);
repaint();
break;
case Canvas.KEY_NUM9:
sb += "9";
System.out.println(sb);
repaint();
break;
case Canvas.KEY_STAR:
sb="";
System.out.println(sb);
repaint();
break;
}
//由于其他按键是action类型 与数字有所区别 所以要再做检测。
int actionType = getGameAction(keyCode);
switch (actionType){
case Canvas.UP:
if(sb.length() == 0){
break;
}else{
sb+="+";
repaint();
break;
}
case Canvas.DOWN:
if(sb.length() == 0){
break;
}else{
sb+="-";
repaint();
break;
}
case Canvas.LEFT:
if(sb.length() == 0){
break;
}else{
sb+="*";
repaint();
break;
}
case Canvas.RIGHT:
if(sb.length() == 0){
break;
}else{
sb+="/";
repaint();
break;
}
//等操作。对数据进行处理。
case Canvas.FIRE:
int num1Temp;
int num2Temp;
float total;
String sTemp = sb.trim();
System.out.println(sTemp);
if(sTemp.indexOf("+")!= -1){
int i = sTemp.indexOf("+");
num1Temp = Integer.parseInt(sTemp.substring(0, i));
num2Temp = Integer.parseInt(sTemp.substring(i+1));
total = num1Temp+num2Temp ;
sb += " = "+String.valueOf(total);
repaint();
break;
}

if(sTemp.indexOf("-")!= -1){
int i = sTemp.indexOf("/");
num1Temp = Integer.parseInt(sTemp.substring(0, i));
num2Temp = Integer.parseInt(sTemp.substring(i+1));
total = num1Temp-num2Temp ;
sb += " = "+String.valueOf(total);
repaint();
break;
}
if(sb.indexOf("*")!= -1){
int i = sTemp.indexOf("*");
num1Temp = Integer.parseInt(sTemp.substring(0, i));
num2Temp = Integer.parseInt(sTemp.substring(i+1));
total = num1Temp * num2Temp ;
sb += " = "+String.valueOf(total);
repaint();
break;
}
if(sb.indexOf("/")!= -1){
int i = sTemp.indexOf("/");
num1Temp = Integer.parseInt(sTemp.substring(0, i));
num2Temp = Integer.parseInt(sTemp.substring(i+1));
total = num1Temp/num2Temp ;
sb += " = "+String.valueOf(total);
repaint();
break;
}
default:
repaint();
break;
}


}

}[/align]

看来格式有点乱 这个没办法 将就着吧。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值