importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.*;
classEditWindowextendsJFrameimplementsActionListener
{
JPopupMenumenu;
JMenuItemitemCopy,itemCut,itemPaste;
JTextAreatext;
EditWindow(Strings)
{
super(s);
setSize(260,270);
setLocation(120,120);
setVisible(true);
menu=newJPopupMenu();
itemCopy=newJMenuItem("拷贝");
itemCut=newJMenuItem("剪切");
itemPaste=newJMenuItem("粘贴");
menu.add(itemCopy);
menu.add(itemCut);
menu.add(itemPaste);
text=newJTextArea();
text.addMouseListener(newMouseAdapter()
{
publicvoidmousePressed(MouseEvente)
{
if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{
menu.show(text,e.getX(),e.getY());
}
}
}
);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Containercon=getContentPane();
con.add(newJScrollPane(text),BorderLayout.CENTER);
con.validate();
validate();
itemCopy.addActionListener(this);
itemCut.addActionListener(this);
itemPaste.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==itemCopy)
text.copy();
elseif(e.getSource()==itemCut)
text.cut();
elseif(e.getSource()==itemPaste)
text.paste();
}
}
publicclassExample
{
publicstaticvoidmain(Stringargs[])
{
EditWindowwin=newEditWindow("窗口");
}