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

📄 texteditor.java

📁 用Eclipse开发J2me书籍的程序源码。
💻 JAVA
字号:
/*
 * TextEditor
 *
 * Created on 2006年4月25日, 下午10:50
 */

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

/**
 *
 * @author  Liu Bin
 * @version 1.0
 */
public class TextEditor extends MIDlet
        implements CommandListener {
    
    //Display管理
    Display display = null;
    
    //TextBox对象
    TextBox tbMain = new TextBox("Text Editor",      //设置标题
            "",                                      //设置内容
            255,                                     //设置最大字符数255
            TextField.ANY);                          //可以接受任何输入
        
    //创建命令按钮
    
    //插入
    Command cmdInsertFirst =
            new Command("Insert at start", Command.SCREEN,1);
    Command cmdInsertCurrent =
        new Command("Insert at current", Command.SCREEN,1);
    
    //删除
    Command cmdDeleteFirstChar =
            new Command("Delete First char", Command.SCREEN,1);
    Command cmdDeleteLastChar =
            new Command("Delete Last char", Command.SCREEN,1);
    Command cmdDeletePreviousChar =
        new Command("Delete previous char", Command.SCREEN,1);
    
    //替换
    Command cmdReplace =
            new Command("Replace", Command.SCREEN,1);
    
    //显示光标位置
    Command cmdCaret =
        new Command("Caret Position", Command.SCREEN,1);
    Command cmdExit = new Command("Exit", Command.EXIT, 2);
    
    //用于插入数字的计数器
    int count = 0;
    
    public void startApp() {
        //设置Displayable对象
        tbMain.setInitialInputMode("MIDP_UPPERCASE_LATIN");
        
        tbMain.addCommand(cmdExit);
        tbMain.addCommand(cmdCaret);
        tbMain.addCommand(cmdInsertFirst);
        tbMain.addCommand(cmdInsertCurrent);

        tbMain.addCommand(cmdDeleteFirstChar);
        tbMain.addCommand(cmdDeleteLastChar);
        tbMain.addCommand(cmdDeletePreviousChar);
        
        tbMain.addCommand(cmdReplace);
        tbMain.setCommandListener(this);
        
        //获得当前MIDlet的Display对象
        display = Display.getDisplay(this);   
        //设置TextBox对象为当前显示对象
        display.setCurrent(tbMain);           
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command c, Displayable d) {
        if (c == this.cmdExit) {
            destroyApp(true);
            notifyDestroyed();
        } else if(c == this.cmdCaret) {
            //光标位置
            int caret = tbMain.getCaretPosition();
            System.out.println("\n\nCaret Position: "+caret);
            tbMain.setTitle("Caret Position: " + String.valueOf(caret));
        }
        else if(c == this.cmdInsertFirst) {
            //在开始处插入一个字符
            String str = Integer.toString(count++);
            tbMain.insert(str, 0);
        } else if (c == this.cmdInsertCurrent) {
            //在当前位置插入字符
            String str = Integer.toString(count++);
            int caret = tbMain.getCaretPosition();
            System.out.println("Caret Position:"+caret);
            System.out.println("Insert:"+str);
            tbMain.insert(str, caret);
        } else if(c == this.cmdReplace) {
            //替换
            String str = Integer.toString(count++);
            tbMain.setChars(str.toCharArray(), 0, str.length());
        }else if(c == this.cmdDeleteFirstChar) {
            //删除第一个字符
            int size = tbMain.size();
            if (size>0) {
                //当没有字符时不能执行该功能
                tbMain.delete(0, 1);
            }
        } else if(c == this.cmdDeleteLastChar) {
            //删除最后一个字符
            int size = tbMain.size();
            if (size>0) {
                //当没有字符时不能执行该功能
                tbMain.delete(size-1, 1);
            }
        } else if(c == this.cmdDeletePreviousChar) {
            //删除前一个字符
            int caret = tbMain.getCaretPosition();
            if (caret > 0) {
                tbMain.delete(caret-1, 1);
            }
        }
    }
}

⌨️ 快捷键说明

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