代码改变世界
2012-08-10 23:49
java线程例子
阅读(793)
评论()
编辑
收藏
举报
- import javax.microedition.midlet.*;
-
- import javax.microedition.lcdui.*;
-
-
-
- public
class CalcMIDlet
extends MIDlet
implements CommandListener {
-
- private CalcForm calcForm;
- private Command cmdExit =
new Command("退出", Command.EXIT,
1);
-
- public
void startApp() {
- Display display = Display.getDisplay(this);
- calcForm = new CalcForm();
- calcForm.addCommand(cmdExit);
- calcForm.setCommandListener(this);
- display.setCurrent(calcForm);
- }
-
- public
void pauseApp() {
-
- }
-
- public
void destroyApp(boolean unconditional) {
-
- }
-
- public
void commandAction(Command cmd, Displayable d) {
- if(cmd == cmdExit) {
- notifyDestroyed();
- }
- }
- }
-
Java代码
- import javax.microedition.lcdui.*;
-
-
- public
class CalcKeyboard
extends CustomItem {
- public
static
final String BACKSPACE =
"<-";
- public
static
final String CE =
"CE";
- public
static
final String C =
"C";
- public
static
final String SYMBOL_MINUS =
"+/-";
- public
static
final String NUM_ZERO =
"0";
- public
static
final String NUM_ONE =
"1";
- public
static
final String NUM_TWO =
"2";
- public
static
final String NUM_THREE =
"3";
- public
static
final String NUM_FOUR =
"4";
- public
static
final String NUM_FIVE =
"5";
- public
static
final String NUM_SIX =
"6";
- public
static
final String NUM_SEVEN =
"7";
- public
static
final String NUM_EIGHT =
"8";
- public
static
final String NUM_NINE =
"9";
- public
static
final String SYMBOL_DOT =
".";
- public
static
final String ADD =
"+";
- public
static
final String MINUS =
"-";
- public
static
final String MULT =
"*";
- public
static
final String DIVIDE =
"/";
- public
static
final String EQUALS =
"=";
-
- private
static
final
int PRESSED =
0;
- private
static
final
int RELEASED =
1;
-
- private CalcKeyboardListener ckListener;
- private Font textFont;
- private
int col;
- private
int row;
- private
int btnWidth;
- private
int btnHeight;
- private
int hSpace =
4;
- private
int vSpace =
4;
-
- private
int 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;
- }
-
- protected
int getMinContentHeight() {
- return row * (btnHeight + vSpace) - vSpace;
- }
-
- protected
int getMinContentWidth() {
- return col * (btnWidth + hSpace) - hSpace;
- }
-
- protected
int getPrefContentHeight(int width) {
- return getMinContentHeight();
- }
-
- protected
int getPrefContentWidth(int height) {
- return getMinContentWidth();
- }
-
- protected
void 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);
- }
- }
-
- private
void 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);
- }
- else
if(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);
- }
-
- private
int getIndex(int x,
int y) {
- int j = x / (btnWidth+hSpace);
- int i = y / (btnHeight+vSpace);
-
- return (col*i)+j;
- }
-
-
-
-
- protected
void pointerPressed(int x,
int y) {
- keyState = PRESSED;
- int ax = x - x % (btnWidth+hSpace);
- int ay = y - y % (btnHeight+vSpace);
- repaint(ax, ay, btnWidth, btnHeight);
- }
-
-
-
-
- protected
void 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]);
- }
- }
-
-
- public
void setCalcKeyboardListener(CalcKeyboardListener ckListener) {
- this.ckListener = ckListener;
- }
- }
-
-
-
- public
interface CalcKeyboardListener {
-
-
-
-
- public
void actionPerformmed(CalcKeyboard ck, String symbol);
- }
-
Java代码
- import javax.microedition.lcdui.*;
-
-
- public
class CalcScreen
extends CustomItem {
- private String text;
- private Font showFont;
-
- public CalcScreen() {
- super(null);
- showFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
- text = "";
- }
-
- protected
int getMinContentHeight() {
- return showFont.getHeight() +
4;
- }
-
- protected
int getMinContentWidth() {
- return showFont.stringWidth("012345678901234.-") +
4;
- }
-
- protected
int getPrefContentHeight(int width) {
- return getMinContentHeight();
- }
-
- protected
int getPrefContentWidth(int height) {
- return
150;
- }
-
- protected
void 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);
- }
-
- public
void setText(String text) {
- this.text = text;
- repaint();
- }
-
- public String getText() {
- return text;
- }
- }