簡單記事本java源碼執行個體_java

來源:互聯網
上載者:User

本文執行個體講述了簡單記事本java實現代碼。分享給大家供大家參考。具體如下:

完整代碼如下:

複製代碼 代碼如下:
import java.awt.*;
import java.io.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
 
public class Main extends Frame implements ActionListener {
        private static final long serialVersionUID = 1L;
TextArea textArea = new TextArea();
   MenuBar menuBar = new MenuBar();
   Menu fileMenu = new Menu("File");
   MenuItem newItem = new MenuItem("New");
   MenuItem openItem = new MenuItem("Open");
   MenuItem saveItem = new MenuItem("Save");
   MenuItem saveAsItem = new MenuItem("Save As");
   MenuItem exitItem = new MenuItem("Exit");
   Menu editMenu = new Menu("Edit");
   MenuItem selectItem = new MenuItem("Select All");
   MenuItem copyItem = new MenuItem("Copy");
   MenuItem cutItem = new MenuItem("Cut");
   MenuItem pasteItem = new MenuItem("Paste");
   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 Main(){
    setTitle("記事本程式-by Jackbase");
    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");
    }
  }
 
    @SuppressWarnings("deprecation")
        public static void main(String[] args){
      Frame frame = new Main(); //建立對象
      frame.show();   //是對象顯示
   }
}

運行結果如下圖所示:

希望本文所述對大家的java程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.