windows剪贴板java程序不工作

eivnm1vs  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(243)
public class ClipBoardListener extends Thread implements ClipboardOwner{

Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();  

  public void run() {
    Transferable trans = sysClip.getContents(this);  
    TakeOwnership(trans);       
    while(true) {
    }
  }  

    public void lostOwnership(Clipboard c, Transferable t) {  

  try {  
      ClipBoardListener.sleep(250);  //waiting e.g for loading huge elements like word's etc.
  } catch(Exception e) {  
    System.out.println("Exception: " + e);  
  }  
  Transferable contents = sysClip.getContents(this);  
    try {
        process_clipboard(contents, c);
    } catch (Exception ex) {
        Logger.getLogger(ClipBoardListener.class.getName()).log(Level.SEVERE, null, ex);
    }
  TakeOwnership(contents);

}  

  void TakeOwnership(Transferable t) {  
    sysClip.setContents(t, this);  
  }  

public void process_clipboard(Transferable t, Clipboard c) { //your implementation
    String tempText;
    Transferable trans = t;

    try {
        if (trans != null?trans.isDataFlavorSupported(DataFlavor.stringFlavor):false) {
            tempText = (String) trans.getTransferData(DataFlavor.stringFlavor);

           addStringToClipBoard(tempText);

        }

    } catch (Exception e) {
    }
}
public static void addStringToClipBoard(String cache) {                         
    StringSelection selection = new StringSelection(cache.trim());
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(selection,null);
}

public static void main(String[] args) {

    ClipBoardListener c = new ClipBoardListener();
    c.start();

 }  
}

英文翻译:
你好,我试着写一个程序,我可以用它来编辑windows剪贴板。我想在插入的时候前面和后面的空格都没有了。
我就是这样实现的。但它不起作用。
示例:复制字符串“hello world”并插入“hello world”。
有人知道吗?谢谢您
德意志银行übersetzung公司:
你好,我有一个很好的程序,我可以用它来打开Windows。我会去的ü我们将继续前进。
我是这样的。这是一个有趣的故事。
beispiel:kopiere diesen字符串“你好世界”和fü通用电气“你好世界”ein。
世界环境学会ß 杰曼德·韦特?丹克

0wi1tuuw

0wi1tuuw1#

注解掉 TakeOwnership 内线 lostOwnership() . 它将重置您刚刚在前一个剪贴板中更改的原始剪贴板内容 process_clipboard 打电话。

//TakeOwnership(contents);

使其非静态/使用“this”:

public void addStringToClipBoard(String cache) {
  StringSelection selection = new StringSelection(cache.trim());
  Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  clipboard.setContents(selection,this);
}

在本例中,您的类不需要是 Thread 你可以这么说 c.run() 直接在 main . 具有紧密cpu循环的方法 while(true) {} 不是一个好主意-等待一些退出条件。

相关问题