How to use freely resizable font&nb

来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 09:12 点击:
Contents [hide]
1 Overview
2 Source code: FontSizingMIDlet.java
3 Source code: FontCanvas.java
4 Example application
5 See also
[edit]
Overview
In MIDlets the font and its properties are specified by using the standard LCDUI javax.microedition.lcdui.Font class. A font has three properties: size, style and face. In MIDP these properties have the following values:
Font size
SIZE_SMALL
SIZE_MEDIUM
SIZE_LARGE

Font style
STYLE_PLAIN
STYLE_BOLD
STYLE_ITALIC
STYLE_UNDERLINED
or a combination of STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED

Font face
FACE_SYSTEM
FACE_MONOSPACE
FACE_PROPORTIONAL

Having only three sizes has been especially a limitation and this has been improved in the latest Java Runtimes for S60. It is now possible to use a new getFont() method in com.nokia.mid.ui.DirectUtils class:
public static Font getFont(int face, int style, int height)
face - one of FACE_SYSTEM, FACE_MONOSPACE, or FACE_PROPORTIONAL
style - STYLE_PLAIN, or a combination of STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED
height - font height in pixels
This improvement is part of Nokia UI API 1.2, which is included in Java Runtime 1.3 for S60. Here is a full working sample MIDlet code below. The FontSizingMIDlet reads shows sample text on the screen and the font size and style can be changed by using the buttons on the touch screen. Because touch screen is used, this MIDlet works only in devices having one(for example, Nokia 5800 XpressMusic and N97).
[edit]
Source code: FontSizingMIDlet.java
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
 
public class FontSizingMIDlet extends MIDlet {
    private FontCanvas canvas;
 
    public void startApp() {
        canvas = new FontCanvas(this);
        Display.getDisplay(this).setCurrent(canvas);
    }
 
    public void pauseApp() {
    }
 
    public void destroyApp(boolean unconditional) {
    }
 
    protected void showError(String title, String text) {
        Alert alert = new Alert(title, text, null, AlertType.ERROR);
        alert.setTimeout(Alert.FOREVER);
        alert.getType().playSound(Display.getDisplay(this));
        Displayable current = Display.getDisplay(this).getCurrent();
        if (current instanceof Alert) {}
        else Display.getDisplay(this).setCurrent(alert);
    }
}

[edit]
Source code: FontCanvas.java
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import com.nokia.mid.ui.DirectUtils;
import com.nokia.mid.ui.TactileFeedback;
 
public class FontCanvas extends Canvas implements CommandListener {
    private FontSizingMIDlet midlet;
    private Command exitCommand;
    private String heightString = "";
    private String fontString = "";
    private Font font;
    private int fontHeight = 20;
 
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Java编程语言的八大优点
    • JVM对象生命周期详细介绍
    • Java平台上的CRM系统
    • Java 算数测试小程序
    • Command(命令模式)
    • Java环境 使用Resin在NT环境下配置JSP环境
    • Java 一个简单的画图程序
    • Java 日历的小程序
    • Java 统计代码的小工具
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1