到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comwww.matrix.org.cn无关,不对该信息负责.通过http://www.matrix.org.cn//resource/article/0/541.html访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
为TextBox组件创建简单的剪贴板(Matrix-corej2me系列)
作者:chris 来源:www.matrix.org.cn  发布时间:2006-02-22 17:48:48.047

    在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查看他的个人信息

 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读