欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Java制作一个简单的记事本

程序员文章站 2024-03-03 15:27:16
通过使用java的swing、io来实现一个简单记事本,实现打开指定的text文本文件,然后将text文件的内容加载到swing组件中,然后在swing组件中编辑记事本内容...

通过使用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();  
        } 
    } 
}

效果图:

使用Java制作一个简单的记事本 

以上就是本文的全部内容了,希望大家能够喜欢。