learning java AWT 剪贴板 传递文本
Posted lianghong881018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了learning java AWT 剪贴板 传递文本相关的知识,希望对你有一定的参考价值。
import javax.swing.*; import java.awt.*; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; public class SimpleCliboard private Frame f = new Frame("test"); private Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); private TextArea jtaCopyTo = new TextArea(5,20); private TextArea jtaPaste = new TextArea(5,20); private Button btCopy = new Button("copy"); private Button btPaste = new Button("paste"); public void init() var p = new Panel(); p.add(btCopy); p.add(btPaste); btCopy.addActionListener(e -> var contents = new StringSelection(jtaCopyTo.getText()); clipboard.setContents(contents,null); ); btPaste.addActionListener(e -> if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) try var content = (String) clipboard.getData(DataFlavor.stringFlavor); jtaPaste.append(content); catch (UnsupportedFlavorException ex) ex.printStackTrace(); catch (IOException ex) ex.printStackTrace(); ); var box = new Box(BoxLayout.X_AXIS); box.add(jtaCopyTo); box.add(jtaPaste); f.add(p,BorderLayout.SOUTH); f.add(box,BorderLayout.CENTER); f.pack(); f.setVisible(true); public static void main(String[] args) new SimpleCliboard().init();
output:
以上是关于learning java AWT 剪贴板 传递文本的主要内容,如果未能解决你的问题,请参考以下文章
learning java AWT 布局管理器CardLayout