2011-07-15 75 views

回答

200

這個工作對我來說,是相當簡單:

導入這些:

import java.awt.datatransfer.*; 
import java.awt.Toolkit; 

然後把這個代碼片段,無論你想改變剪貼板:

String myString = "This text will be copied into clipboard when running this code!"; 
StringSelection stringSelection = new StringSelection(myString); 
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard(); 
clpbrd.setContents(stringSelection, null); 

我剛剛嘗試過它,它的工作原理。不確定是否存在一些「良好實踐」缺陷,但它會解決您的問題!

LH

+1

我們可以setContents()與所有者太http://stackoverflow.com/questions/3591945/copying-to-clipboard-in-java –

+0

@AquariusPower似乎傳遞'stringSelection'作爲第二參數'setContents(..)',使它成爲'ClipboardOwner',就像在鏈接的答案中一樣,沒有任何意義:查看[source](http://grepcode.com/file/repository.grepcode.com/java /root/jdk/openjdk/8u40-b25/java/awt/datatransfer/StringSelection.java#StringSelection.lostOwnership%28java.awt.datatransfer.Clipboard%2Cjava.awt.datatransfer.Transferable%29),ClipboardOwner的唯一方法'它實現了'lostOwnership(..)',它是空的。所以,第二個參數似乎是一個完全可選的回調。 –

+0

@EvgeniSergeev它可能是有用的,如果我們擴展StringSelection :) –

10

下面的類允許你爲一個字符串從剪貼板複製/粘貼/。

import java.awt.AWTException; 
import java.awt.HeadlessException; 
import java.awt.Robot; 
import java.awt.Toolkit; 
import java.awt.datatransfer.Clipboard; 
import java.awt.datatransfer.DataFlavor; 
import java.awt.datatransfer.StringSelection; 
import java.awt.datatransfer.UnsupportedFlavorException; 
import java.awt.event.KeyEvent; 
import java.io.IOException; 

public class SystemClipboard 
{ 
    public static void copy(String text) 
    { 
     Clipboard clipboard = getSystemClipboard(); 

     clipboard.setContents(new StringSelection(text), null); 
    } 

    public static void paste() throws AWTException 
    { 
     Robot robot = new Robot(); 

     robot.keyPress(KeyEvent.VK_CONTROL); 
     robot.keyPress(KeyEvent.VK_V); 
     robot.keyRelease(KeyEvent.VK_CONTROL); 
     robot.keyRelease(KeyEvent.VK_V); 
    } 

    public static String get() throws Exception 
    { 
     Clipboard systemClipboard = getSystemClipboard(); 
     DataFlavor dataFlavor = DataFlavor.stringFlavor; 

     if (systemClipboard.isDataFlavorAvailable(dataFlavor)) 
     { 
      Object text = systemClipboard.getData(dataFlavor); 
      return (String) text; 
     } 

     return null; 
    } 

    private static Clipboard getSystemClipboard() 
    { 
     Toolkit defaultToolkit = Toolkit.getDefaultToolkit(); 
     Clipboard systemClipboard = defaultToolkit.getSystemClipboard(); 

     return systemClipboard; 
    } 
} 
+1

可能需要一些粘貼OS X的邏輯,因爲它很特殊。 –

3

這是寫在一個裝飾的方式接受的答案:

Toolkit.getDefaultToolkit() 
     .getSystemClipboard() 
     .setContents(
       new StringSelection(txtMySQLScript.getText()), 
       null 
     ); 
1

我發現這樣做,所以你可以從txtbox獲得輸入或有什麼更好的方式在文本中生成框,並能夠點擊一個按鈕來做到這一點。這裏是複製到剪貼板的代碼(2017/2018)!

import java.awt.datatransfer.*; 
import java.awt.Toolkit; 

private void /* Action performed when the copy to clipboard button is clicked */ { 
    String ctc = txtCommand.getText().toString(); 
    StringSelection stringSelection = new StringSelection(ctc); 
    Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard(); 
    clpbrd.setContents(stringSelection, null); 
} 

// txtCommand is the variable of a text box 
+1

希望這有助於! – ElementCodez

+0

「2017/2018」是什麼意思? Java的特定版本?你使用哪個版本? –

相關問題