问题描述
/***@(#)EditorFrame.java**EditorFrameapplication**@author*@version1.002011/3/19*/importjava.lang.*;importjava.awt.*;importjava.io.*;importjava.awt.datatransfer.*;importjava.awt.event.*;publicclassEditorFrameextendsFrameimplementsActionListenter{TextAreatextArea=newTextArea();MenuBarmenuBar=newMenuBar();MenufileMenu=newMenu("File");MenuItemnewItem=newMenuItem("New");MenuItemopenItem=newMenuItem("Open");MenuItemsaveItem=newMenuItem("Save");MenuItemsaveAsItem=newMenuItem("SaveAs");MenuItemexitItem=newMenuItem("Exit");MenueditMenu=newMenu("Edit");MenuItemselectItem=newMenuItem("SelectAll");MenuItemcopyItem=newMenuItem("Copy");MenuItemcutItem=newMenuItem("Cut");MenuItempasteItem=newMenuItem("Paste");StringfileName="NoName";ToolkittoolKit=Toolkit.getDefaultToolkit();ClipboardClipBoard=toolKit.getSystemClipboard();privateFileDialogopenFileDialog=newFileDialog(this,"OpenFile",FileDialog.LOAD);privateFileDialogsaveAsFileDialog=newFileDialog(this,"SaveFileAs",FileDialog.SAVE);publicEditorFrame(){setTitle("私密日记本");setFont(newFont("TimesNewRoman",Font.PLAIN,12));setBackground(Color.white);setSize(400,300);fileMenu.add(newItem);fileMenu.add(openItem);fileMenu.add(saveItem);fileMenu.add(saveAsItem);fileMenu.addSeparator();fileMenu.add(exitItem);editMenu.add(selectItem);editMenu.addSeparator();editMenu.add(copyItem);editMenu.add(cutItem);editMenu.add(pasteItem);menuBar.add(fileMenu);menuBar.add(editMenu);setMenuBar(menuBar);add(textArea);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});newItem.addActionListener(this);openItem.addActionListener(this);saveItem.addActionListener(this);saveAsItem.addActionListener(this);exitItem.addActionListener(this);selectItem.addActionListener(this);copyItem.addActionListener(this);cutItem.addActionListener(this);pasteItem.addActionListener(this);}publicvoidactionPerformed(ActionEvente){ObjecteventSource=e.getSource();if(eventSource==newItem){textArea.setText("");}elseif(eventSource==openItem){openFileDialog.show();fileName=openFileDialog.getDirectory()+openFileDialog.getFile();if(fileName!=null)readFile(fileName);}elseif(eventSource==saveItem){if(fileName!=null)writeFile(fileName);}elseif(eventSource==saveAsItem){saveAsFileDialog.show();fileName=saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();if(fileName!=null)writeFile(fileName);}elseif(eventSource==copyItem){Stringtext=textArea.getSelectedText();StringSelectionselection=newStringSelection(text);ClipBoard.setContents(selection,null);}elseif(eventSource==cutItem){Stringtext=textArea.getSelectedText();StringSelectionselection=newStringSelection(text);ClipBoard.setContents(selection,null);textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());}elseif(eventSource==pasteItem){Transferablecontents=ClipBoard.getContents(this);if(contents==null)return;Stringtext;text="";try{text=(String)contents.getTransferData(DataFlavor.stringFlavor);}catch(Exceptionexception){}textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd());}elseif(eventSource==exitItem){System.exit(0);}}publicvoidreadFile(StringfileName){try{Filefile=newFile(fileName);FileReaderreadln=newFileReader(file);intsize=(int)file.length();intcharsRead=0;char[]content=newchar[size];while(readln.ready())charsRead+=readln.read(content,charsRead,size-charsRead);readln.close();textArea.setText(newString(content,0,charsRead));}catch(Exceptione){System.out.println("Erroropenningfile");}}publicvoidwriteFile(StringfileName){try{Filefile=newFile(fileName);FileWriterwriteOut=newFileWriter(file);writeOut.write(textArea.getText());writeOut.close();}catch(IOExceptione){System.out.println("Errorwritingfile");}}publicstaticvoidmain(String[]args){Frameframe=newEditorFrame();frame.show();//TODO,addyourapplicationcodeSystem.out.println("HelloWorld!");}}请教高手,为什么运行时会出现无法将java.awt.MenuItem中的addActionListener(java.awt.event.ActionListener)应用于(EditorFrame)
解决方案
解决方案二:
看的我头都大了
解决方案三:
publicclassEditorFrameextendsFrameimplementsActionListenter{ActionListener写错了
解决方案四:
真正的扭摆的人敢于无视注释的存在!
解决方案五:
看的太痛苦