Java小程序的复制粘贴功能怎么实现不了,求大神
import java.awt.*;import java.awt.event.*;
import *;
public class menu
{
public static void main(String args[])
{
mytextedit te=new mytextedit("简单文本编辑器");
}
}
class mytextedit extends Frame implements ActionListener,MouseListener,ItemListener
{
TextArea text;
String str="";
CheckboxMenuItem miFontBold,miFontItalic;
PopupMenu popM;
int style=Font.PLAIN;
public mytextedit(String s)
{
super(s);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
Menu mn1=new Menu("文件");
MenuItem miOpen=new MenuItem("打开",new MenuShortcut(KeyEvent.VK_O));
MenuItem miSave=new MenuItem("保存",new MenuShortcut(KeyEvent.VK_S));
MenuItem miExit=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_X));
mn1.add(miOpen);
mn1.add(miSave);
mn1.addSeparator();//分割行
mn1.add(miExit);
miOpen.addActionListener(this);
miSave.addActionListener(this);
miExit.addActionListener(this);
Menu mn2=new Menu("编辑");
MenuItem miCopy=new MenuItem("拷贝",new MenuShortcut(KeyEvent.VK_C));
MenuItem miPaste=new MenuItem("粘贴",new MenuShortcut(KeyEvent.VK_V));
mn2.add(miCopy);
mn2.add(miPaste);
mn2.addSeparator();
miCopy.addActionListener(this);
miPaste.addActionListener(this);
Menu miFont=new Menu("字体");
CheckboxMenuItem miFontBold=new CheckboxMenuItem("黑体");
CheckboxMenuItem miFontItalic=new CheckboxMenuItem("斜体");
miFont.add(miFontBold);
miFont.add(miFontItalic);
miFontBold.addItemListener(this);
miFontItalic.addItemListener(this);
mn2.add(miFont);
MenuBar mb=new MenuBar();
mb.add(mn1);
mb.add(mn2);
setMenuBar(mb);
popM=new PopupMenu();
MenuItem mipopOpen=new MenuItem("打开文件");
MenuItem mipopSave=new MenuItem("保存文件");
popM.add(mipopOpen);
popM.add(mipopSave);
mipopOpen.addActionListener(this);
mipopSave.addActionListener(this);
text=new TextArea();
text.add(popM);
text.addMouseListener(this);
add(text,BorderLayout.CENTER);
setSize(400,300);
setVisible(true);
}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e)
{
//如果在文本区中单击右键,则在鼠标位置处弹出菜单popM
if(e.isPopupTrigger())//判断用户是否要求弹出菜单
popM.show((Component)e.getSource(),e.getX(),e.getY());
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{
//以下程序分别处理各项菜单命令
String strcmd=e.getActionCommand();
if(strcmd.equals("退出"))
System.exit(0);
try
{
if(strcmd.equals("打开")||strcmd.equals("打开文件"))
openText();
if(strcmd.equals("保存")||strcmd.equals("保存文件"))
saveText();
}catch(IOException e1){}
if(strcmd.equals("拷贝"))
str=text.getSelectedText();
if(str.equals("粘贴"))
text.insert(str,text.getCaretPosition());
}
public void itemStateChanged(ItemEvent e)
{
Font font=new Font("宋体",style,12);
//取得事件源,这里一定是个复选菜单项
CheckboxMenuItem cmi=(CheckboxMenuItem)e.getSource();
//如果是黑体菜单处于选中状态
if(cmi.getState()&&cmi.getLabel().equals("黑体"))
style+=Font.BOLD;
//如果是黑体菜单处于非选中状态
else if(!cmi.getState()&&cmi.getLabel().equals("黑体"))
style-=Font.BOLD;
//如果斜体菜单处于选中状态
if(cmi.getState()&&cmi.getLabel().equals("斜体"))
style+=Font.ITALIC;
//如果斜体菜单处于非选中状态
if(!cmi.getState()&&cmi.getLabel().equals("斜体"))
style-=Font.ITALIC;
String s=text.getText();
text.setFont(new Font("宋体",style,12));
text.setText(s);
}
//打开文件并将内容显示在文本区的过程
public void openText() throws IOException
{
/*****************/
FileDialog fdopen = new FileDialog(this,"打开",FileDialog.LOAD);//框属性为"LOAD加载",附于JFrame对象
fdopen.setVisible(true);
BufferedReader in = new BufferedReader(new FileReader(fdopen.getDirectory()+fdopen.getFile()));//选择路径,为文件流对象
String str = null;
//整行读取
while ((str = in.readLine()) != null){
text.append(str + "\n");
}
in.close();
}
//保存文本区的内容
public void saveText() throws IOException
{
/*************/
FileDialog fd = new FileDialog(this,"另存为",FileDialog.SAVE);
fd.setVisible(true);
FileOutputStream out = new FileOutputStream(fd.getDirectory()+fd.getFile()+".txt");
String str = text.getText();
out.write(str.getBytes());//String的getBytes()将一个字符串转换化为一个字节数组
out.close();
}
}