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

📄 formdemo.java

📁 人民邮电出版社的《J2ME手机开发入门》全部源代码
💻 JAVA
字号:
/*
 * FormDemo.java
 *
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author  Liu Bin
 * @version
 */
public class FormDemo extends MIDlet
        implements CommandListener, ItemStateListener {
    private Display display;
    
    private Form form;
    //定义使用的命令按钮
    private Command cmdExit;
    
    public FormDemo() {
        try {
            nbInit();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    private void nbInit() throws Exception {
        cmdExit = new Command("退出", Command.EXIT, 0);
        form = new Form("Form演示");
        form.addCommand(cmdExit);
        form.setCommandListener(this);
        form.setItemStateListener(this);
        
        //直接添加字符串
        form.append("修改下面的选项时,程序会打印出相关信息");
        
        //添加Gauge
        Gauge g = new Gauge("Volume", true, 10, 5);
        form.append(g);
        
        
        //添加单选按钮
        String str[] = {"大号字体", "中号字体", "小号字体", "系统缺省"};
        ChoiceGroup radioGroup = new ChoiceGroup("选择字体:",
                ChoiceGroup.EXCLUSIVE, str, null);
        radioGroup.setSelectedIndex(3, true);
        form.append(radioGroup);
        
        
        //添加StringItem
        StringItem ai = new StringItem("StringItem标签:", "StringItem内容");
        form.append(ai);
        
        
        
        //添加下拉列表框
        String strPopup[] = {"颜色", "字体", "图形"};
        ChoiceGroup poppupGroup = new ChoiceGroup("请选择字体式样:",
                ChoiceGroup.POPUP, strPopup, null);
        form.append(poppupGroup);
        
        //添加复选按钮
        String strCheckBox[] = {"粗体", "倾斜", "下划线"};
        ChoiceGroup cgi = new ChoiceGroup("请选择字体式样:",
                ChoiceGroup.MULTIPLE, strCheckBox, null);
        form.append(cgi);
    }
    
    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command cmd, Displayable d) {
        if (cmd == cmdExit) {
            destroyApp(true);
        }
    }
    
    /**
     * 处理Item状态改变
     */
    public void itemStateChanged(Item item) {
        StringBuffer sb = new StringBuffer();
        sb.append("当前Item对象标签:" + item.getLabel());
        sb.append("\n当前Item类名:" + item.getClass().getName());
        sb.append("\n当前Item对象最小宽度:" + item.getMinimumWidth());
        sb.append("\n当前Item对象最小高度:" + item.getMinimumHeight());
        sb.append("\n当前Item对象推荐宽度:" + item.getPreferredWidth());
        sb.append("\n当前Item对象推荐高度:" + item.getPreferredHeight());
        
        if (item instanceof StringItem) {
            StringItem si = (StringItem)item;
            sb.append("\n--------Item内容:" + si.getText());
        } else if (item instanceof ChoiceGroup) {
            ChoiceGroup cg = (ChoiceGroup)item;
            boolean[] selectedArray_return = new boolean[cg.size()];
            cg.getSelectedFlags(selectedArray_return);
            for (int i=0;i<cg.size();i++) {
                if (cg.isSelected(i)) {
                    sb.append("\n--------第" + i +
                            "个选项被选中: " + cg.getString(i));
                }
            }
        } else if (item instanceof Gauge) {
            Gauge g = (Gauge)item;
            sb.append("\n--------Gauge当前值:" + g.getValue());
        }
        
        System.out.println(sb.toString());
    }
}

⌨️ 快捷键说明

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