快捷搜索:

为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. Muchowhttp://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)

源代码下载:

下载

关于翻译作者:

bruceyuki,JAVA C#技巧喜欢者,现就读于新西兰奥克兰大年夜学,正介入大年夜学的一个AI项目,可以点击http://www.matrix.org.cn/user_view.asp?username=bruceyuki查看他的小我信息

您可能还会对下面的文章感兴趣: