⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listfont.java

📁 用Eclipse开发J2me书籍的程序源码。
💻 JAVA
字号:
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class ListFont extends MIDlet implements CommandListener {
    private Display display;

    // 定义主菜单的内容
    private static String menuItems[] = {
        "Input Text", 
        "Satic Text", 
        "More..." 
    };

    // ============================================主菜单
    private List lstMainMenu = new List(
            "Please Select Font",   // 标题
            List.IMPLICIT,          // 单选类型
            menuItems,              // 设置列表内容
            null);                  // 列表不使用图标

    // ============================================选择More以后的二级菜单
    private static String menuItemsMore[] = {
        "Face", 
        "Size", 
        "Style" 
    };

    private List lstMore = new List(
            "Please Select Font Properties", // 标题
            List.IMPLICIT,                   // 单选类型
            menuItemsMore,                   // 设置列表内容
            null);                           // 列表不使用图标

    // ============================================三级菜单 - 选择Face
    private static String menuItemsFace[] = {
        "FACE_MONOSPACE",
        "FACE_PROPORTIONAL", 
        "FACE_SYSTEM" 
    };

    private List lstFace = new List(
            "Please Select Font Face",  // 标题
            List.EXCLUSIVE,             // 单选类型
            menuItemsFace,              // 设置列表内容
            null);                      // 列表不使用图标

    // ============================================三级菜单 - 选择Size
    private static String menuItemsSize[] = {
        "SIZE_LARGE", 
        "SIZE_MEDIUM",
         "SIZE_SMALL"
    };

    private List lstSize = new List(
            "Please Select Font Face",  // 标题
            List.EXCLUSIVE,             // 单选类型
            menuItemsSize,              // 设置列表内容
            null);                      // 列表不使用图标

    // ============================================三级菜单 - 选择Style
    private static String menuItemsStyle[] = {
        "STYLE_BOLD", 
        "STYLE_ITALIC",
        "STYLE_PLAIN ", 
        "STYLE_UNDERLINED"
    };

    private List lstStyle = new List(
            "Please Select Font Style", // 标题
            List.MULTIPLE,              // 单选类型
            menuItemsStyle,             // 设置列表内容
            null);                      // 列表不使用图标

    // OK
    private Command cmdOk = new Command("Ok", Command.OK, 1);

    // BACK
    private Command cmdBack = new Command("Back", Command.BACK, 1);

    // Exit
    private Command cmdExit = new Command("Exit", Command.EXIT, 1);

    // 当前字体
    private Font font;

    public ListFont() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void startApp() throws MIDletStateChangeException {
        // 设置命令监听
        lstMainMenu.addCommand(cmdExit);
        lstMainMenu.setCommandListener(this);

        lstMore.addCommand(cmdBack);
        lstMore.setCommandListener(this);

        lstFace.addCommand(cmdOk);
        lstFace.addCommand(cmdBack);
        lstFace.setCommandListener(this);

        lstSize.addCommand(cmdOk);
        lstSize.addCommand(cmdBack);
        lstSize.setCommandListener(this);

        lstStyle.addCommand(cmdOk);
        lstStyle.addCommand(cmdBack);
        lstStyle.setCommandListener(this);

        // 获得默认的字体
        font = Font.getDefaultFont();
        // 根据当前字体设置具体字体属性
        setFontProperties();

        // 获得当前MIDlet的Display对象
        display = Display.getDisplay(this);
        // 设置List对象lstMainMenu为当前显示对象
        display.setCurrent(lstMainMenu);
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void destroyApp(boolean arg0) 
        throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command c, Displayable d) {
        List list = (List) d;

        if (c == List.SELECT_COMMAND) {
            // 获得被选中选项的索引
            int index = list.getSelectedIndex();

            if (list == this.lstMainMenu) {
                switch (index) {
                case 0: // Input Text
                    font = Font.getFont(Font.FONT_INPUT_TEXT);
                    updateFont();
                    break;
                case 1:
                    font = Font.getFont(Font.FONT_STATIC_TEXT);
                    updateFont();
                    break;
                case 2:
                    display.setCurrent(this.lstMore);
                }
            } else if (list == this.lstMore) {
                switch (index) {
                case 0:
                    display.setCurrent(this.lstFace);
                    break;
                case 1:
                    display.setCurrent(this.lstSize);
                    break;
                case 2:
                    display.setCurrent(this.lstStyle);
                }
            }
        } else if (c == this.cmdBack) {
            if (list == this.lstMore) {
                display.setCurrent(this.lstMainMenu);
            } else {
                display.setCurrent(this.lstMore);
            }
        } else if (c == this.cmdOk) {
            
            int face = font.getFace();
            System.out.println("Old Face: " + face);
            int size = font.getSize();
            System.out.println("Old Size: " + size);
            int style = font.getStyle();
            System.out.println("Old Style: " + style);

            if (list == this.lstFace) {
                //用户设置了字体Face
                switch (list.getSelectedIndex()) {
                case 0:
                    face = Font.FACE_MONOSPACE;
                    break;
                case 1:
                    face = Font.FACE_PROPORTIONAL;
                    break;
                case 2:
                    face = Font.FACE_SYSTEM;
                }
            } else if (list == this.lstSize) {
                //用户设置了字体size
                //int s = 0;
                switch (list.getSelectedIndex()) {
                case 0:
                    size = Font.SIZE_LARGE;
                    break;
                case 1:
                    size = Font.SIZE_MEDIUM;
                    break;
                case 2:
                    size = Font.SIZE_SMALL;
                }  
            } else if (list == this.lstStyle) {
                //用户设置了字体Style
                style = 0;
                if (this.lstStyle.isSelected(0)) {
                    style |= Font.STYLE_BOLD;
                }
                if (this.lstStyle.isSelected(1)) {
                    style |= Font.STYLE_ITALIC;
                }
                if (this.lstStyle.isSelected(3)) {
                    style |= Font.STYLE_UNDERLINED;
                }
            }
            
            font = Font.getFont(face, style, size);
            updateFont();
            display.setCurrent(this.lstMore);
        } else if (c == this.cmdExit) {
            notifyDestroyed();
        }
    }

    private void updateFont() {
        System.out.println("New Face: " + font.getFace());
        System.out.println("New Size: " + font.getSize());
        System.out.println("New Style: " + font.getStyle());
        
        for (int i = 0; i < this.lstMainMenu.size(); i++) {
            this.lstMainMenu.setFont(i, font);
        }

        for (int i = 0; i < this.lstMore.size(); i++) {
            this.lstMore.setFont(i, font);
        }

        for (int i = 0; i < this.lstFace.size(); i++) {
            this.lstFace.setFont(i, font);
        }

        for (int i = 0; i < this.lstSize.size(); i++) {
            this.lstSize.setFont(i, font);
        }

        for (int i = 0; i < this.lstStyle.size(); i++) {
            this.lstStyle.setFont(i, font);
        }
    }

    /**
     * 根据当前字体设置具体字体属性
     */
    private void setFontProperties() {
        //设置Face
        switch (font.getFace()) {
        case Font.FACE_MONOSPACE:
            this.lstFace.setSelectedIndex(0, true);
            break;
        case Font.FACE_PROPORTIONAL:
            this.lstFace.setSelectedIndex(1, true);
            break;
        case Font.FACE_SYSTEM:
            this.lstFace.setSelectedIndex(2, true);
        }

        //设置Size
        switch (font.getSize()) {
        case Font.SIZE_LARGE:
            this.lstSize.setSelectedIndex(0, true);
            break;
        case Font.SIZE_MEDIUM:
            this.lstSize.setSelectedIndex(1, true);
            break;
        case Font.SIZE_SMALL:
            this.lstSize.setSelectedIndex(2, true);
        }
        
        //设置Style
        this.lstStyle.setSelectedIndex(0, font.isBold());
        this.lstStyle.setSelectedIndex(1, font.isItalic());
        this.lstStyle.setSelectedIndex(2, font.isPlain());
        this.lstStyle.setSelectedIndex(3, font.isUnderlined());

    }

}

⌨️ 快捷键说明

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