包含開啟檔案,儲存檔案,複製,粘貼,剪下功能,都是在功能表列目上實現的功能
原始碼:
import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.filechooser.FileFilter;import static java.awt.event.InputEvent.*;import java.awt.datatransfer.*;import java.util.Enumeration;import java.util.Hashtable;/*import javax.swing.*;import javax.swing.filechooser.FileFilter;import java.awt.event.*;import java.awt.*;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;*/class jishiben { public static void main(String args[]) { TextEdit TE=new TextEdit("記事本"); }}class TextEdit extends Frame implements ActionListener{ MenuBar m; Menu m1,m2; MenuItem xinjian,dakai,baocun,tuichu,jianqie,fuzhi,zhantie; TextArea text; String filename; FileDialog openFD,saveFD; BufferedReader in; FileReader read; BufferedWriter out; FileWriter writer; Clipboard cb; TextEdit(String s) { super(s); m=new MenuBar(); m1=new Menu("檔案"); xinjian=new MenuItem("建立"); dakai=new MenuItem("開啟"); baocun=new MenuItem("儲存"); tuichu=new MenuItem("退出"); m2=new Menu("編輯"); jianqie=new MenuItem("剪下"); fuzhi=new MenuItem("複製"); zhantie=new MenuItem("粘貼"); text=new TextArea(); openFD=new FileDialog(this,"開啟",FileDialog.LOAD); saveFD=new FileDialog(this,"儲存",FileDialog.SAVE); filename="NoName"; m1.add(xinjian); m1.addSeparator(); m1.add(dakai); m1.addSeparator(); m1.add(baocun); m1.addSeparator(); m1.add(tuichu); m2.add(jianqie); m2.addSeparator(); m2.add(fuzhi); m2.addSeparator(); m2.add(zhantie); m.add(m1); m.add(m2); //關鍵區段,沒有為cb申請記憶體,下面對cb操作會出錯 cb = new Clipboard("nothing"); // setMenuBar(m); setSize(300,400);setVisible(true); add(text,"Center"); xinjian.addActionListener(this); dakai.addActionListener(this); baocun.addActionListener(this); tuichu.addActionListener(this); jianqie.addActionListener(this); fuzhi.addActionListener(this); zhantie.addActionListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if(e.getSource()==xinjian) { text.setText(""); } if(e.getSource()==dakai) { openFD.show(); String s; filename=openFD.getDirectory()+openFD.getFile(); if(filename!=null) { try { File file=new File(filename); read=new FileReader(file); in=new BufferedReader(read); while((s=in.readLine())!=null) text.append(s+'\n'); in.close(); read.close(); } catch(IOException e2){} } } if(e.getSource()==baocun) { saveFD.show(); filename=saveFD.getDirectory()+saveFD.getFile(); if(filename!=null) { try { File file=new File(filename); writer=new FileWriter(file); out=new BufferedWriter(writer); out.write(text.getText(),0,(text.getText()).length()); out.close(); writer.close(); } catch(IOException e2){} } } if(e.getSource()==tuichu) { System.exit(0); } if(e.getSource()==jianqie) { //類text中沒有cut方法,不能使用text.cut String s=text.getSelectedText(); StringSelection select=new StringSelection(s); cb.setContents(select,null); text.replaceRange("",text.getSelectionStart(),text.getSelectionEnd()); } if(e.getSource()==fuzhi) { //同上,沒有copy這個方法 String s=text.getSelectedText(); StringSelection select=new StringSelection(s); cb.setContents(select,null); } if(e.getSource()==zhantie) { //同上,沒有paste方法 String s=""; Transferable t = cb.getContents(null); try { if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) { // 因為原系的剪貼簿裡有多種資訊, 如文字, 圖片, 檔案等 // 先判斷開始取得的可傳輸的資料是不是文字, 如果是, 取得這些文字 s = (String)t.getTransferData(DataFlavor.stringFlavor); // 同樣, 因為Transferable中的DataFlavor是多種類型的, // 所以傳入DataFlavor這個參數, 指定要取得哪種類型的Data. //System.out.println(s); } } catch (UnsupportedFlavorException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } text.insert(s,text.getCaretPosition()); } } }