JAVA计算器【源码】
来源:互联网 发布:js数组slice 编辑:程序博客网 时间:2024/05/01 00:32
- import javax.microedition.midlet.*;
-
- import javax.microedition.lcdui.*;
-
-
- publicclass CalcMIDletextends MIDletimplements CommandListener {
-
- private CalcForm calcForm;
- private Command cmdExit =new Command("退出", Command.EXIT,1);
-
- publicvoid startApp() {
- Display display = Display.getDisplay(this);
- calcForm = new CalcForm();
- calcForm.addCommand(cmdExit);
- calcForm.setCommandListener(this);
- display.setCurrent(calcForm);
- }
-
- publicvoid pauseApp() {
-
- }
-
- publicvoid destroyApp(boolean unconditional) {
-
- }
-
- publicvoid commandAction(Command cmd, Displayable d) {
- if(cmd == cmdExit) {
- notifyDestroyed();
- }
- }
- }
-
Java代码
- import javax.microedition.lcdui.*;
-
-
- publicclass CalcKeyboardextends CustomItem {
- publicstaticfinal String BACKSPACE ="<-";
- publicstaticfinal String CE ="CE";
- publicstaticfinal String C ="C";
- publicstaticfinal String SYMBOL_MINUS ="+/-";
- publicstaticfinal String NUM_ZERO ="0";
- publicstaticfinal String NUM_ONE ="1";
- publicstaticfinal String NUM_TWO ="2";
- publicstaticfinal String NUM_THREE ="3";
- publicstaticfinal String NUM_FOUR ="4";
- publicstaticfinal String NUM_FIVE ="5";
- publicstaticfinal String NUM_SIX ="6";
- publicstaticfinal String NUM_SEVEN ="7";
- publicstaticfinal String NUM_EIGHT ="8";
- publicstaticfinal String NUM_NINE ="9";
- publicstaticfinal String SYMBOL_DOT =".";
- publicstaticfinal String ADD ="+";
- publicstaticfinal String MINUS ="-";
- publicstaticfinal String MULT ="*";
- publicstaticfinal String DIVIDE ="/";
- publicstaticfinal String EQUALS ="=";
-
- privatestaticfinalint PRESSED =0;
- privatestaticfinalint RELEASED =1;
-
- private CalcKeyboardListener ckListener;
- private Font textFont;
- privateint col;
- privateint row;
- privateint btnWidth;
- privateint btnHeight;
- privateint hSpace =4;
- privateint vSpace =4;
-
- privateint keyState = RELEASED;
-
- private String[] keyLabel = {
- BACKSPACE, CE, C, SYMBOL_MINUS,
- NUM_SEVEN, NUM_EIGHT, NUM_NINE, DIVIDE,
- NUM_FOUR, NUM_FIVE, NUM_SIX, MULT,
- NUM_ONE, NUM_TWO, NUM_THREE, MINUS,
- NUM_ZERO, SYMBOL_DOT, EQUALS, ADD
- };
-
- public CalcKeyboard(int col,int row) {
- super(null);
- textFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
- this.col = col;
- this.row = row;
- btnHeight = textFont.getHeight() + 4;
- btnWidth = btnHeight + 10;
- }
-
- protectedint getMinContentHeight() {
- return row * (btnHeight + vSpace) - vSpace;
- }
-
- protectedint getMinContentWidth() {
- return col * (btnWidth + hSpace) - hSpace;
- }
-
- protectedint getPrefContentHeight(int width) {
- return getMinContentHeight();
- }
-
- protectedint getPrefContentWidth(int height) {
- return getMinContentWidth();
- }
-
- protectedvoid paint(Graphics g,int w,int h) {
- for(int i=0; i<keyLabel.length; i++) {
- drawButton(g, keyLabel[i], i%col * (btnWidth+hSpace), i/col*(btnHeight+vSpace), btnWidth, btnHeight);
- }
- }
-
- privatevoid drawButton(Graphics g, String str,int x,int y,int w,int h) {
- g.setColor(160, 160, 255);
- g.drawRect(x, y, w-1, h-1);
- if(keyState == RELEASED) {
- g.setColor(240,240, 255);
- }
- elseif(keyState == PRESSED) {
- g.setColor(210, 210, 255);
- }
- g.fillRect(x+2, y+2, w-4, h-4);
-
- g.setColor(0, 0,0);
- g.setFont(textFont);
- g.drawString(str, x+w/2, y+h, Graphics.BOTTOM|Graphics.HCENTER);
- }
-
- privateint getIndex(int x,int y) {
- int j = x / (btnWidth+hSpace);
- int i = y / (btnHeight+vSpace);
-
- return (col*i)+j;
- }
-
-
-
-
- protectedvoid pointerPressed(int x,int y) {
- keyState = PRESSED;
- int ax = x - x % (btnWidth+hSpace);
- int ay = y - y % (btnHeight+vSpace);
- repaint(ax, ay, btnWidth, btnHeight);
- }
-
-
-
-
- protectedvoid pointerReleased(int x,int y) {
- keyState = RELEASED;
- int ax = x - x % (btnWidth+hSpace);
- int ay = y - y % (btnHeight+vSpace);
- repaint(ax, ay, btnWidth, btnHeight);
- if(ckListener !=null) {
- int index = getIndex(x, y);
- ckListener.actionPerformmed(this, keyLabel[index]);
- }
- }
-
-
- publicvoid setCalcKeyboardListener(CalcKeyboardListener ckListener) {
- this.ckListener = ckListener;
- }
- }
-
-
-
- publicinterface CalcKeyboardListener {
-
-
-
-
- publicvoid actionPerformmed(CalcKeyboard ck, String symbol);
- }
-
Java代码
- import javax.microedition.lcdui.*;
-
-
- publicclass CalcScreenextends CustomItem {
- private String text;
- private Font showFont;
-
- public CalcScreen() {
- super(null);
- showFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
- text = "";
- }
-
- protectedint getMinContentHeight() {
- return showFont.getHeight() +4;
- }
-
- protectedint getMinContentWidth() {
- return showFont.stringWidth("012345678901234.-") +4;
- }
-
- protectedint getPrefContentHeight(int width) {
- return getMinContentHeight();
- }
-
- protectedint getPrefContentWidth(int height) {
- return150;
- }
-
- protectedvoid paint(Graphics g,int w,int h) {
- g.setColor(160,160, 255);
- g.drawRect(0, 0, w-1, h-1);
- g.setColor(210,210, 255);
- g.drawRect(2, 2, w-5, h-5);
-
- g.setColor(0, 0,0);
- g.setFont(showFont);
- g.drawString(text, w-10, h-3, Graphics.BOTTOM|Graphics.RIGHT);
- }
-
- publicvoid setText(String text) {
- this.text = text;
- repaint();
- }
-
- public String getText() {
- return text;
- }
- }