calcscreen.java

来自「J2ME程序设计实例教程的源码」· Java 代码 · 共 51 行

JAVA
51
字号
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;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?