`
- 浏览:
3481795 次
- 性别:
- 来自:
杭州
-
- 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;
- }
- }
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Java 计算器 源码,实现简单的运算,是Java的GUI编程的入门经典实例。涉及到了基本的界面布局,事件监听和处理等基本知识,新手好好利用它,加油!
JAVA计算器源码JAVA计算器源码
源码描述: 一、源码介绍 1、可以简单的进行计算(加减乘除平方根),开发环境(JDK1.8,无数据库) 二、注意事项 1、 开发语言:Java,无数据库
java计算器源码 java计算器源码下载
java计算器,期末大作业,各项功能都还可以!
JAVA 计算器源码 JAVA 计算器源码JAVA 计算器源码
java计算器源码,仿windows的计算器java计算器源码,仿windows的计算器
Java计算器源码.docxJava计算器源码.docxJava计算器源码.docxJava计算器源码.docxJava计算器源码.docxJava计算器源码.docxJava计算器源码.docxJava计算器源码.docx
java 计算器源码
Java-计算器源码 Java写的计算器源码 包含加减乘除, 括号等运算 此代码仅供学习 不得用于任何商业用途 谢谢大家合作
Java 计算器源码 jar包内压有源码文件
java计算器 有源代码 可以运行 jdk 1.6 java计算器 实现功能教齐全的计算器 初学者用 用初学者用初学者用初学者用初学者用初学者用初学者用
Java计算器源码 源码描述: 一、源码介绍 1、可以简单的进行计算(加减乘除平方根),开发环境(JDK1.8,无数据库) 二、注意事项 1、 开发语言:Java,无数据库
一个简单的java计算器源码JiSuanQi.java