服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

为textbox组件创建简单的剪贴板


  在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, 的内容。
  
  源代码:
  [code]/*--------------------------------------------------
  * 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();
  }
  }
  }

扫描关注微信公众号