import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.DataFlavor;
複製到剪貼簿(copy to clipboard):
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); StringSelection data = new StringSelection("Copy string"); clipboard.setContents(data, data);
貼上剪貼簿內容(paste from clipboard):
Transferable pasteData = clipboard.getContents(clipboard); if (pasteData != null) { try { if (pasteData.isDataFlavorSupported(DataFlavor.stringFlavor)) { String s = (String)(pasteData.getTransferData(DataFlavor.stringFlavor)); //output s } } catch (UnsupportedFlavorException ex) { ex.printStackTrace(); } catch (IOException ex1) { ex1.printStackTrace(); } }
範例(example):
package SwingDemo; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; /*** * * @author Duran * * Clipboard Demo * */ public class ClipboardExample { private static JFrame frame; public static void main(String[] args) { frame = new JFrame("Group Layout Demo"); frame.setSize(400, 200); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); JLabel lbl1 = new JLabel("lbl1"); JLabel lbl2 = new JLabel("lbl2"); final JTextArea tf1 = new JTextArea(); tf1.setRows(3); tf1.setText("copy me"); final JTextArea tf2 = new JTextArea(); tf2.setRows(3); //Clipboard example final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); JButton jb1 = new JButton("cpoy"); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //***************************************************************** //copy string StringSelection data = new StringSelection(tf1.getText()); clipboard.setContents(data, data); //***************************************************************** } }); JButton jb2 = new JButton("paste"); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //***************************************************************** Transferable pasteData = clipboard.getContents(clipboard); if (pasteData != null) { try { if (pasteData.isDataFlavorSupported(DataFlavor.stringFlavor)) { tf2.setText( (String)(pasteData.getTransferData(DataFlavor.stringFlavor))); } } catch (UnsupportedFlavorException ex) { ex.printStackTrace(); } catch (IOException ex1) { ex1.printStackTrace(); } } //***************************************************************** } }); //group layout layout.setHorizontalGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup() .addComponent(lbl1) .addComponent(lbl2)) .addGroup(layout.createParallelGroup() .addComponent(tf1) .addComponent(tf2)) .addGroup(layout.createParallelGroup() .addComponent(jb1) .addComponent(jb2)) ); layout.setVerticalGroup(layout.createSequentialGroup() .addGap(10) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(lbl1) .addComponent(tf1) .addComponent(jb1)) .addGap(10) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(lbl2) .addComponent(tf2) .addComponent(jb2)) .addGap(10) ); //BorderLayout frame.getContentPane().add(BorderLayout.CENTER,panel); frame.setVisible(true); } }
--
如果覺得文章不錯,請幫忙點選廣告,
謝謝!
0 留言