J2ME Application Example - A simple calculator implementation (attachment code)

xiaoxiao2021-03-06  40

J2ME Application Example - A simple calculator implementation (attachment code)

Using the low-level user interface, a simple calculator program based on the Nokia S60 series, I hope to help everyone. This article only contains program code, if you need a picture file and source code, please send me an email: cqucyf@263.net

The source code is as follows

// Calcmidlet.java

Package my.calc;

Import javax.microedition.midlet. *;

Import javax.microedition.lcdui. *;

Public class cagcmidlet extends midlet {

Private static cagcmidlet installation;

Private Calccanvas Calc = New Calccanvas ();

/ ** constructor * /

Public cagcmidlet () {

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.ffullcanvas;

Import javax.microedition.lcdui. *;

Import java.io. *;

/ **

* Calculator interface

*

Title: Calculator based on Nokia S60

*

description:

*

Copyright: Copyright (c) 2005

*

company:

* email: cqucyf@263.net

* @version 1.0

* /

Public Class Calccanvas Extends Fullcanvas {

/ ** Exit button * /

Private command cmdexit;

/**title*/

Private string title = "Calculator";

/ ** font * /

PRIVATE FONT F;

/ ** Digital * /

PRIVATE long result = 0;

/ ** Focus number * /

Private int Num = 0;

/ ** Focus coordinate * /

Private int focusx = 5;

PRIVATE INT FOCUSY = 52;

/ ** Button width interval * /

Private int intervalWidth = 2;

Private int interfaceHeight = 2;

/ ** picture starting coordinate * /

Private INT X = 10;

PRIVATE INT Y = 70;

/ ** Button size * /

Private int buttonWidth = 37;

Private int buttonHeight = 28;

/ ** image file * /

PRIVATE Image = NULL;

/ ** Store control represented content * /

Private char [] info = {'7', '8', '9', '/',

'4', '5', '6', '*',

'1', '2', '3', '-',

'0', '?', '=', ' '

}

/ ** The first number in the operation * /

Private long firstnum = 0;

/ ** The second number in the operation * /

PRIVATE long secondnum = 0;

/ ** Do you enter the first number, True represents have entered, FALSE does not enter * /

PRIVATE BOOLEAN flag = false;

/ ** Store the subscript of the prescribed arithmetic button * /

Private int func = 0;

/ ** Default constructor * /

Public cagccanvas () {

Try {

Image = image.createImage ("/ CALC.PNG");

//System.out.println (X);

} catch (ioexception e) {

// ignore

System.out.println (e);

}

}

/ **

* Draw

* @Param G graphic object

* /

Public void paint (graphics g) {

/ ** Clear the current display * /

G.SetColor (255, 255, 255);

G.fillRect (0, 0, getWidth (), getHeight ());

// Restore system color

G.SetColor (0,0,0);

f = g.getfont ();

// Draw the title

g.drawstring (title, getwidth () / 2, f.getHeight () / 2, graphics.top | graphics.hcenter;

// Draw a text box

g.drawRect (3, 24, 172, 20);

// Draw a number

String temp = long.toString (result);

g.drawstring (Temp, getWidth () - 3, 30, graphics.top | graphics.right);

// Draw panel

G.DrawImage (Image, 2, 50, graphics.top | graphics.Left);

// Draw a focus

Drawfocus (g, num);

}

/ **

* Event processing

* @Param KeyCode button value

* /

Public void keypressed (int keycode) {

//System.out.println (Keycode);

/ / Move left to the left, if you move to the right boundary, move to the next line

if (Keycode == KEY_LEFT_ARROW) {

/ / Judgment whether to reach the last one

IF (NUM == 0) {

Num = 15;

} else {

Num--

}

}

/ / Move right to right, if moved to the left boundary, move to the end of the previous line

IF (Keycode == key_right_arrow) {

/ / Judgment whether to reach the first

IF (NUM == 15) {

Num = 0;

} else {

Num ;

}

}

/ / Move up, if moved to the upper boundary, move to the lower boundary corresponding location

if (Keycode == Key_up_arrow) {// Determines if it moves to the upper boundary, if it is the corresponding position to the lower boundary

IF (NUM - 4 <0) {

NUM = 15 Num - 3;

} else {

Num = NUM ​​- 4;

}

}

/ / Move down, if moved to the lower boundary, move to the upper boundary corresponding location

IF (Keycode == KEY_DOWN_ARROW) {

/ / Judgment whether to reach the lower boundary, if it is moved to the corresponding position of the upper boundary

IF (NUM 4> 15) {

Num = NUM ​​ 3 - 15;

} else {

Num = NUM ​​ 4;

}

}

//Confirm button

IF (Keycode == -5) {

/ / Operation according to the different button

Action ();

}

//System.out.println ("num: " Num);

// Draw

Repaint ();

}

/ **

* Draw a focus rectangle

* @Param G graphic object

* @Param NUM Focus number

* /

Private void DrawFocus (Graphics G, Int Num) {

/ / Set the color of the color is red

G.SetColor (255, 0, 0);

/ / Set the X and Y coordinates of the focus

SetxandyBynum (NUM);

// Draw the selection box

g.drawRect (Focusx, Focusy, ButtonWidth, ButtonHeight);

}

/ **

* Set the X and Y coordinates of focus according to focus

* @Param NUM Focus number

* /

Private void setxandyBynum (int Num) {

FOCUSX = 5 (Num% 4) * (ButtonWidth Intervalwidth);

FOCUSY = 52 (Num / 4) * (ButtonHeight IntervalHeight);

}

/ **

* After the user presses the OK button, operate according to the different button

* /

Private void action () {

/ / Judgment the focus button is a number

IF ((NUM> = 0 && Num <3) || (Num> 3 && Num <7) || (Num> 7 && Num <11) || (NUM == 12)) {

// If you are a number, multiply the original number by 10, plus the current number

Result = Result * 10 (Info [NUM] - '0');

}

/ / If the negative is pressed, the negative number of the current number is

IF (Num == 13) {

Result = -Result;

}

/ / Judgment whether the first number is input

IF (Flag == true) {

Secondnum = result;

// Process function button

/ / Press the = button, or press the operation button to calculate

IF (NUM == 14 || (Num% 4 == 3)) {

/ / According to the calculation button

IF (Func == 3) {

// divide the method

Result = firstnum / secondnum;

} else if (func == 7) {

// Multiplication

Result = firstnum * secondnum;

} else IF (func == 11) {

// subtracting calculation result = firstnum - secondnum;

Else IF (func == 15) {

// addition operation

Result = firstnum secondnum;

}

IF (NUM == 14) {

// Assign the calculation result to the first number

Firstnum = result;

/ / Modify the flag to false

Flag = false;

} else {

// Record the operation button

Func = NUM;

// Assign the calculation result to the first number

Firstnum = result;

// will display clear

Result = 0;

}

}

} else {

// Assign the current value to the first number

Firstnum = result;

/ / Determine if the operation button is pressed and store the prescribed operation button

IF (NUM% 4 == 3) {

Func = NUM;

/ / Set the first number has been entered, and will display clear

Flag = true;

Result = 0;

}

}

}

}

Welcome everyone to visit my blog: http://blog.9cbs.net/mailbomb

转载请注明原文地址:https://www.9cbs.com/read-72747.html

New Post(0)