站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题为TextBox组件创建简单的剪贴板(Matrix-corej2me系列)

为TextBox组件创建简单的剪贴板(Matrix-corej2me系列)

摘要: 为TextBox组件创建简单的剪贴板(Matrix-corej2me系列)
内容: 在Core J2ME Technology and MIDP一书中许多的可以在多个多行编辑组件(multiple TextBox)中共享剪贴板数据的例子。

注意:这个例子基于MIDP与CLDC 1.0.3版
本文得到作者John W. Muchow的允许,部分引用了Core J2ME Technology and MIDP, Sun Microsystems与Prentice Hall出版社 2002 Sun Microsystems Inc, 的内容。

源代码:

/*--------------------------------------------------
* SimpleClipBoard.java
*
* Example from the book: Core J2ME Technology
* Copyright John W. Muchow http://www.CoreJ2ME.com
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class SimpleClipBoard extends MIDlet implements CommandListener
{
private Display display; // 对显示对象的引用Reference to Display object
private TextBox tbClip; // 主TextBox组件Main textbox
private Command cmExit; // 退出命令Command to exit
private Command cmStartMark; // 开始标记命令Command to start marking a block
private Command cmCopy; // 复制剪贴板命令Command to copy to clipboard
private Command cmPaste; // 粘贴剪贴板命令Command to paste into textbox
private int beginOffset = 0; // 复制的开始索引The start index of copy
private char[] clipBoard = null; // 剪贴板The clipboard
private int clipBoardChars = 0; // 记录剪贴板中的字符Number of chars in clipboard

public SimpleClipBoard()
{
display = Display.getDisplay(this);

// Create the Commands. Notice the priorities assigned
cmExit = new Command("Exit", Command.EXIT, 1);
cmStartMark = new Command("Mark", Command.SCREEN, 2);
cmCopy = new Command("Copy", Command.SCREEN, 3);
cmPaste = new Command("Paste", Command.SCREEN, 4);

tbClip = new TextBox("Clip Board", "Tee to grn", 15, TextField.ANY);
tbClip.addCommand(cmExit);
tbClip.addCommand(cmStartMark);
tbClip.addCommand(cmCopy);
tbClip.addCommand(cmPaste);
tbClip.setCommandListener(this);

// Allocate a clipboard big enough to hold the entire textbox
clipBoard = new char[tbClip.getMaxSize()];
}

public void startApp()
{
display.setCurrent(tbClip);
}

public void pauseApp()
{
}

public void destroyApp(boolean unconditional)
{
}

public void commandAction(Command c, Displayable s)
{
if (c == cmStartMark)
{
beginOffset = tbClip.getCaretPosition();
}
else if (c == cmCopy && (tbClip.getCaretPosition() > beginOffset))
{
// Allocate an array to hold the current textbox contents
char[] chr = new char[tbClip.size()];

// Get the current textbox contents
tbClip.getChars(chr);

// The count of characters in the clipboard
clipBoardChars = tbClip.getCaretPosition() - beginOffset;

// Copy the text into the clipboard
// arraycopy(source, sourceindex, dest, destindex, count)
System.arraycopy(chr, beginOffset, clipBoard, 0, clipBoardChars);
}
else if (c == cmPaste)
{
// Make sure the paste will not overrun the textbox length.
if ((tbClip.size() + clipBoardChars) <= tbClip.getMaxSize())
tbClip.insert(clipBoard, 0, clipBoardChars, tbClip.getCaretPosition());
}
else if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}
}


源代码下载:
下载

关于翻译作者:
bruceyuki,JAVA C#技术爱好者,现就读于新西兰奥克兰大学,正参与大学的一个AI项目,可以点击http://www.matrix.org.cn/user_view.asp?username=bruceyuki查看他的个人信息

↑返回目录
前一篇: 在组件(components)之间共享Ticker
后一篇: 深入MIDIetStateChangeException