package test;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
public class T_menu extends JFrame implements MouseListener,ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
PopupMenu pm;
JTextArea text;
JPanel panel;
MenuItem[] menuitem;
java.awt.datatransfer.Clipboard clipboard=java.awt.Toolkit.getDefaultToolkit().getSystemClipboard(); //获得系统剪切板
StringSelection stringSelection;
public T_menu(){
menuitem=new MenuItem[4];
menuitem[0]=new MenuItem("复制");
menuitem[1]=new MenuItem("剪切");
menuitem[2]=new MenuItem("粘贴");
menuitem[3]=new MenuItem("退出");
pm=new PopupMenu("pm");
for(int i=0;i<4;i++){
pm.add(menuitem[i]);
menuitem[i].addActionListener(this);
if(i==2)
pm.addSeparator();//加分隔线
}
add(pm);
text=new JTextArea(10,35);
panel=new JPanel();
panel.add(text);
add(panel);
setVisible(true);
setLocation(330,300);
setSize(450,350);
setDefaultCloseOperation(2);
addMouseListener(this);
text.addMouseListener(this);
}
public void mouseClicked(MouseEvent mec){
System.out.println("here now");
if(mec.getModifiers()==mec.BUTTON3_MASK)
{ System.out.println("here now");
pm.show(this,mec.getX(),mec.getY());
}
}
public void mousePressed(MouseEvent mec){}
public void mouseReleased(MouseEvent mec){}
public void mouseExited(MouseEvent mec){}
public void mouseDragged(MouseEvent mec){}
public void mouseEntered(MouseEvent mec){}
public void actionPerformed(ActionEvent e){
if(e.getSource()==menuitem[0]){//复制
String temp=text.getSelectedText();
stringSelection=new StringSelection(temp);
clipboard.setContents(stringSelection,null);
System.out.println(temp);
}
if(e.getSource()==menuitem[1]){//剪切
String temp=text.getSelectedText();
stringSelection=new StringSelection(temp);
clipboard.setContents(stringSelection,null);
int start=text.getSelectionStart();
int end=text.getSelectionEnd();
text.replaceRange("",start,end);
}
if(e.getSource()==menuitem[2]){//粘贴
java.awt.datatransfer.Transferable transferable=clipboard.getContents(this);
java.awt.datatransfer.DataFlavor flavor=java.awt.datatransfer.DataFlavor.stringFlavor;
if(transferable.isDataFlavorSupported(flavor))
{
try
{
int start=text.getSelectionStart();
int end=text.getSelectionEnd();
text.replaceRange("",start,end);
String str;
int n=text.getCaretPosition();
str=(String)transferable.getTransferData(flavor);
text.insert(str,n);
}
catch(Exception ee)
{
}
}
}
if(e.getSource()==menuitem[3]){//退出
System.exit(0);
}
}
public static void main(String agr[]){
T_menu menu=new T_menu();
}
}
/*
*java.awt.datatransfer.Clipboard clipboard=java.awt.Toolkit.getDefaultToolkit().getSystemClipboard(); //获得系统剪切板
//下面是我的记事本部分功能.e.getSource()是当触发了某个事件.这里是copy的代码,下面依此是粘贴,等代码.可以看你需要的部分.(关于clopboard的).都是汉语拼音命名的变良.
else if(e.getSou
rce()==fuZhi||e.getSource()==fuZhi2)//////////Copy 'fuZhi' is 'Copy'
{
String temp=text.getSelectedText();
stringSelection=new StringSelection(temp);
clipboard.setContents(stringSelection,null);
}
else if(e.getSource()==jianQie||e.getSource()==jianQie2)//////////'jianQie' is 'Cut'
{
String temp=text.getSelectedText();
stringSelection=new StringSelection(temp);
clipboard.setContents(stringSelection,null);
int start=text.getSelectionStart();
int end=text.getSelectionEnd();
text.replaceRange("",start,end);
}
//这里是粘贴.主要是如何从系统剪切板里取得数据.
else if(e.getSource()==zhanTie||e.getSource()==zhanTie2)///////////////'zhanTie' is 'Paste'.
{
java.awt.datatransfer.Transferable transferable=clipboard.getContents(this);
java.awt.datatransfer.DataFlavor flavor=java.awt.datatransfer.DataFlavor.stringFlavor;
if(transferable.isDataFlavorSupported(flavor))
{
try
{
int start=text.getSelectionStart();
int end=text.getSelectionEnd();
text.replaceRange("",start,end);
String str;
int n=text.getCaretPosition();
str=(String)transferable.getTransferData(flavor);
text.insert(str,n);
}
catch(Exception ee)
{
}
}
}
java.awt.datatransfer.Transferable transferable=clipboard.getContents(this);//传送操作提供数据的类定义接口.放在剪切板的东西都实现了这个接口.
java.awt.datatransfer.DataFlavor flavor=java.awt.datatransfer.DataFlavor.stringFlavor//在数据拖放期间,或在文件系统中,当每个实例显示在剪切板上时,它代表了数据格式的不透明概念.其实就是在系统内存中的东西是哪种类型.比如:文本,图片等.就象网页中的contentType="text/html"等一样.这里的stringFlavor是字符类型.
if(transferable.isDataFlavorSupported(flavor))//如果是XX类型.这里是把flavor传递进去.就是:如果是文本类型.那么我们才取得:
str=(String)transferable.getTransferData(flavor);
这样把剪切伴东西get出来.才强制成String 型不会出错.
*/