⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clipboardtest.java

📁 java2图形设计卷1:awt 源码
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;

public class ClipboardTest extends    Applet 
                           implements ClipboardOwner {
	private Clipboard clipboard;
	private TextField copyFrom;
	private TextArea  copyTo;
	private Button    copy, paste;

	public void init() {
		// Obtain a reference to the system clipboard
		clipboard = getToolkit().getSystemClipboard();

		copyFrom  = new TextField(20);
		copyTo    = new TextArea(3, 20);
		copy      = new Button("Copy To System Clipboard");
		paste     = new Button("Paste From System Clipboard");

		add(copyFrom);
		add(copy);
		add(paste);
		add(copyTo);

		copy.addActionListener (new CopyListener());
		paste.addActionListener(new PasteListener());
	}
	class CopyListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			// Wrap the data in a transferable object
			StringSelection contents = 
						new StringSelection(copyFrom.getText());

			// Place the transferable onto the clipboard	
			clipboard.setContents(contents, ClipboardTest.this);
		}
	}
	class PasteListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			Transferable contents = clipboard.getContents(this);

			// Determine if data is available in string flavor 
			if(contents != null && 
			   contents.isDataFlavorSupported(
			   				DataFlavor.stringFlavor)) {
				try {
					String string;

					// Have contents cough up string
					string = (String) contents.getTransferData(
									  DataFlavor.stringFlavor);
					copyTo.append(string);
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	public void lostOwnership(Clipboard    clip, 
	                          Transferable transferable) {
		System.out.println("Lost ownership");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -