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

简单记事本java源码实例

程序员文章站 2024-03-02 12:26:58
本文实例讲述了简单记事本java实现代码。分享给大家供大家参考。具体如下: 完整代码如下: 复制代码 代码如下:import java.awt.*; import j...

本文实例讲述了简单记事本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源码实例

希望本文所述对大家的java程序设计有所帮助。