WEB开发网
开发学院WEB开发Jsp Java课Editor 阅读

Java课Editor

 2009-10-28 20:58:28 来源:WEB开发网   
核心提示:view plaincopy to clipboardPRint?package MyFile;import java.awt.*;import java.io.*;import java.awt.datatransfer.*;import java.awt.event.*;public class Editor ex
view plaincopy to clipboardPRint?
package MyFile;  
import java.awt.*;  
import java.io.*;  
import java.awt.datatransfer.*;  
import java.awt.event.*;  
 
public class Editor extends Frame implements ActionListener {  
  TextArea textArea = new TextArea();  
  MenuBar menuBar = new MenuBar();  
  Menu fileMenu = new Menu("文件");  
  MenuItem newItem = new MenuItem("新建");  
  MenuItem openItem = new MenuItem("打开");  
  MenuItem saveItem = new MenuItem("保存");  
  MenuItem saveAsItem = new MenuItem("另存为");  
  MenuItem exitItem = new MenuItem("退出");  
  Menu editMenu = new Menu("编辑");  
  MenuItem selectItem = new MenuItem("全选");  
  MenuItem copyItem = new MenuItem("复制");  
  MenuItem cutItem = new MenuItem("剪切");  
  MenuItem pasteItem = new MenuItem("粘贴");  
  String fileName = null;  
  Toolkit toolKit=Toolkit.getDefaultToolkit(); //工具箱  
  Clipboard clipBoard=toolKit.getSystemClipboard();//剪切  
   
  private FileDialog openFileDialog= new FileDialog(this,"Open File",FileDialog.LOAD);  
  private FileDialog saveAsFileDialog= new FileDialog(this,"Save File As",FileDialog.SAVE);  
 
 public Editor(){  
  setFont(new Font("Times New Roman",Font.PLAIN,12));  
  setBackground(Color.white);  
  setSize(400,300);  
  fileMenu.add(newItem);  
  fileMenu.add(openItem);  
  fileMenu.addSeparator();  
  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(new WindowAdapter(){  
           public void windowClosing(WindowEvent e){  
             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);  
 }  
 public void actionPerformed(ActionEvent e) {  
  Object eventSource = e.getSource();  
  if(eventSource == newItem){  
   textArea.setText("");  
  }else if(eventSource == openItem){  
   openFileDialog.show();  
   fileName = openFileDialog.getDirectory()+openFileDialog.getFile();  
   if(fileName != null)  
    readFile(fileName);  
  }else if (eventSource == saveItem){  
   if(fileName != null)  
    writeFile(fileName);  
  }else if(eventSource == saveAsItem){  
   saveAsFileDialog.show();  
   fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();  
   if (fileName!= null)  
    writeFile(fileName);  
  }else if(eventSource == selectItem){  
    textArea.selectAll();  
  }else if(eventSource == copyItem){  
    String text=textArea.getSelectedText();  
    StringSelection selection=new StringSelection(text);  
    clipBoard.setContents(selection,null);  
  }else if(eventSource == cutItem){  
    String text=textArea.getSelectedText();  
    StringSelection selection=new StringSelection(text);  
    clipBoard.setContents(selection,null);  
    textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());  
  }else if(eventSource == pasteItem){  
    Transferable contents=clipBoard.getContents(this);  
    if(contents==null) return;  
    String text;  
    text="";  
    try{  
      text=(String)contents.getTransferData(DataFlavor.stringFlavor);//剪切内容传给字符串  
    }catch(Exception exception){  
    }  
    textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd());  
  }else if(eventSource == exitItem){  
   System.exit(0);  
  }  
 }  
 
 public void readFile(String fileName){  
  try{  
   File file = new File(fileName);  
   FileReader readIn = new FileReader(file);  
   int size = (int)file.length();  
   int charsRead = 0;  
   char[] content = new char[size];  
   while(readIn.ready())  
    charsRead += readIn.read(content, charsRead, size - charsRead);  
   readIn.close();  
   textArea.setText(new String(content, 0, charsRead));  
  }  
  catch(IOException e){  
   System.out.println("Error opening file");  
  }  
 }  
 public void writeFile(String fileName){  
  try{  
   File file = new File (fileName);  
   FileWriter writeOut = new FileWriter(file);  
   writeOut.write(textArea.getText());  
   writeOut.close();  
  }  
  catch(IOException e){  
   System.out.println("Error writing file");  
  }  
 }  
    
}  

Tags:Java Editor

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接