使用Java製作一個簡單的記事本_java

來源:互聯網
上載者:User

通過使用Java的Swing、IO來實現一個簡單記事本,實現開啟指定的text文字檔,然後將text檔案的內容載入到Swing組件中,然後在Swing組件中編輯記事本內容,然後同菜單的儲存選項將編輯後的內容儲存到text檔案中。代碼如下:

複製代碼 代碼如下:

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
/**
*功能:簡單記事本
*2011-12-25
*/
public class SimpleNotepad implements ActionListener{
    private Frame frame;
    private FileDialog fd_load;
    private FileDialog fd_save;
    private TextArea ta;
    private String file = "";
    private MenuItem save;
    private RandomAccessFile raf;
    private FileChannel fci;
    private FileLock flock;
    private CharsetEncoder encoder;
    private CharsetDecoder decoder;
    public static void main( String args[]) {
        new SimpleNotepad().init();
    }
    public void init(){
        frame = new Frame("My Notepad");
        MenuBar mb = new MenuBar();
        Menu file = new Menu("檔案");
        Menu help = new Menu("協助");
        MenuItem open = new MenuItem("開啟");
        save = new MenuItem("儲存");
        save.setEnabled(false);
        file.add(open);
        file.add(save);
        mb.add(file);
        mb.add(help);     
        frame.setMenuBar(mb);
        ta = new TextArea();
        frame.add(ta,"Center");   
        open.addActionListener(this);
        save.addActionListener(this);
        frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            } 
        }); 
        frame.setSize(600,400);
        frame.setLocation(300,100);
        frame.setVisible( true);     
        fd_load = new FileDialog(frame,"開啟檔案",FileDialog.LOAD);
        fd_save = new FileDialog(frame,"儲存檔案",FileDialog.SAVE);
        Charset charset = Charset.forName(System.getProperty("file.encoding"));
        encoder = charset.newEncoder();
        decoder = charset.newDecoder();
    }
    public void actionPerformed(ActionEvent e){
        String s = e.getActionCommand();
        if(s.equals("開啟")){
            fd_load.setVisible(true);
            String d = fd_load.getDirectory();
            String f = fd_load.getFile();
            if((d != null) && (f != null)){
                String destfile = d + f;
                if(destfile.equals(file)){
                    return;
                }else{
                    this.closeFile();
                    file = destfile;
                    this.loadFile();
                } 
            }
        }else if(s.equals("儲存")){
            this.saveFile();
        }
    } 
    public void loadFile(){
        try{
            raf = new RandomAccessFile(file,"rw");
            fci = raf.getChannel();
            flock = fci.tryLock();
            if(flock == null){
                ta.setText("");
                JOptionPane.showMessageDialog(null,
                    "檔案正在使用中,無法以獨佔的方式開啟!",
                    "錯誤提示", JOptionPane.ERROR_MESSAGE);  
                file = "";
                raf.close();
                raf = null;
            }else{
                int length = (int)fci.size();
                ByteBuffer bb = ByteBuffer.allocate(length);
                fci.read(bb);
                bb.flip();
                CharBuffer cb = decoder.decode(bb);
                ta.setText(cb.toString());
                frame.setTitle("My Notepad - " + file);
                save.setEnabled(true);
            }
        }catch(IOException e){
            e.printStackTrace();  
        }
    }
    public void saveFile(){
        String content = ta.getText();
        try{
            CharBuffer cb = CharBuffer.wrap(content.toCharArray());
            ByteBuffer bb = encoder.encode(cb);
            raf.setLength(0);
            fci.write(bb);
            fci.force(true);
        }catch(IOException e){
            e.printStackTrace();  
        }
    }
    public void closeFile(){
        try{
            if(flock != null){
                flock.release();
            }
            if(raf != null){
                raf.close();  
            }
            file = "";
            frame.setTitle("My Notepad");
            save.setEnabled(false);
        }catch(IOException e){
            e.printStackTrace();  
        } 
    } 
}

效果圖:

 

以上就是本文的全部內容了,希望大家能夠喜歡。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.