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

📄 jiantie.java

📁 用awt组件来实现剪贴板的复制
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
class Win extends Frame implements ActionListener
{
	MenuBar menubar;
	Menu menu;
	MenuItem copy,cut,paste;
	TextArea text1,text2;
	Clipboard clipboard=null;
	Win()
	{
		clipboard=getToolkit().getSystemClipboard();//获取系统剪贴板
		menubar=new MenuBar();
		menu=new Menu("Edit");
		copy=new MenuItem("copy");
		cut=new MenuItem("cut");
		paste=new MenuItem("paste");
		text1=new TextArea(10,20);
		text2=new TextArea(10,20);
		copy.addActionListener(this);
		cut.addActionListener(this);
		paste.addActionListener(this);
		setLayout(new FlowLayout());
		menubar.add(menu);
		menu.add(copy);
		menu.add(cut);
		menu.add(paste);
		setMenuBar(menubar);
		add(text1);
		add(text2);
		setBounds(100,100,400,350);
		setVisible(true);
		validate();
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==copy)					//复制到剪贴板
		{
			String temp=text1.getSelectedText();//得到鼠标拖动选取的文本
			StringSelection text=new StringSelection(temp);
			clipboard.setContents(text, null);
		}
		else if(e.getSource()==cut)
		{
			String temp=text1.getSelectedText();
			StringSelection text=new StringSelection(temp);
			clipboard.setContents(text, null);
			int start=text1.getSelectionStart();
			int end=text1.getSelectionEnd();
			text1.replaceRange("", start, end);
		}
		else if(e.getSource()==paste)
		{
			Transferable contents=clipboard.getContents(this);
			DataFlavor flavor=DataFlavor.stringFlavor;
			if(contents.isDataFlavorSupported(flavor));
				try{
					String str;
					str=(String)contents.getTransferData(flavor);
					text2.append(str);
				}
				catch(Exception ee){}
		}
	}
}
public class jiantie {
	public static void main(String[] args) {
		Win win=new Win();
	}

}

⌨️ 快捷键说明

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