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

📄 textboxdemo.java

📁 《J2ME图形应用基础》中的例子
💻 JAVA
字号:
/*
 * TextBoxDemo.java
 *
 * Created on 2005年3月5日, 下午10:50
 */

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

/**
 *
 * @author  Liu Bin
 * @version 1.0
 */
public class TextBoxDemo extends MIDlet
        implements CommandListener {
    
    //Display管理
    Display display = null;
    
    //TextBox对象
    TextBox tb = new TextBox("TextBox功能演示",         //设置标题
            "文本可能会超出屏幕能够的显示的数量," +      //设置内容
            "在不同的手机上也可能显示出不同的效果," +
            "这些跟设备硬件密切相关",
            100,                                        //设置最大字符数
            TextField. ANY);                            //可以接受任何输入
    
    //Ticker对象
    Ticker ticker = new Ticker("这是滚动文字");
    
    //创建命令按钮
    static final Command cmdInsertFirst =
            new Command("在开始插入", Command.ITEM,1);
    static final Command cmdDeleteBeforeChar =
            new Command("删除第一个字符", Command.ITEM,1);
    static final Command cmdDeleteAfterChar =
            new Command("删除最后一个字符", Command.ITEM,1);
    static final Command cmdReplaceLast =
            new Command("替换", Command.ITEM,1);
    
    static final Command cmdGetString =
            new Command("文字内容", Command.ITEM,1);
    static final Command cmdCaretPosition =
            new Command("获得脱字符号位置", Command.ITEM,1);
    static final Command cmdSize =
            new Command("文本大小", Command.ITEM,1);
    
    static final Command cmdExit = new Command("退出", Command.STOP, 2);
    
    int count = 0;
    
    public void startApp() {
        //设置Displayable对象
        tb.addCommand(cmdExit);
        tb.addCommand(cmdReplaceLast);
        tb.addCommand(cmdInsertFirst);
        tb.addCommand(cmdDeleteBeforeChar);
        tb.addCommand(cmdDeleteAfterChar);
        tb.addCommand(cmdCaretPosition);
        tb.addCommand(cmdSize);
        tb.addCommand(cmdGetString);
        tb.setCommandListener(this);
        
        //显示滚动条
        tb.setTicker(ticker);
        
        display = Display.getDisplay(this); //获得当前MIDlet的Display对象
        display.setCurrent(tb);           //设置tb对象为当前显示对象
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command c, Displayable d) {
        String label = c.getLabel();
        if (label.equals("退出")) {
            destroyApp(true);
        } else if(label.equals("在开始插入")) {
            //在当前位置插入一个数字
            String str = Integer.toString(count++);
            tb.insert(str, 0);
        } else if(label.equals("替换")) {
            //替换最后的字符
            String str = Integer.toString(count++);
            tb.setChars(str.toCharArray(), 0, str.length());
        }else if(label.equals("删除第一个字符")) {
            //删除第一个字符
            int size = tb.size();
            if (size>0) {
                //当没有字符时不能执行该功能
                tb.delete(0, 1);
            }
        } else if(label.equals("删除最后一个字符")) {
            //删除最后一个字符
            int size = tb.size();
            if (size>0) {
                //当没有字符时不能执行该功能
                tb.delete(size-1, 1);
            }
        } else if(label.equals("获得脱字符号位置")) {
            //在标题栏显示光标的位置
            tb.setTitle("当前脱字符号位置:" + tb.getCaretPosition());
        } else if(label.equals("文字内容")) {
            //获得文本框的输入内容
            ticker.setString(tb.getString());
        } else if(label.equals("文本大小")) {
            //在标题上显示文本的大小
            tb.setTitle("当前文本大小为:" + tb.size());
        }
    }
}

⌨️ 快捷键说明

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