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

Java实现的简易记事本

程序员文章站 2024-03-02 20:57:58
本文实例讲述了java实现的简易记事本。分享给大家供大家参考。具体如下: 感觉这个没有自己以前用windows api写的好看了。。。 jdk version : 1....

本文实例讲述了java实现的简易记事本。分享给大家供大家参考。具体如下:

感觉这个没有自己以前用windows api写的好看了。。。

jdk version : 1.7.0

效果如下图所示:

Java实现的简易记事本

Java实现的简易记事本

Java实现的简易记事本

源代码如下:

import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 
/** 
 * the main window 
 * @author neo smith 
 */ 
class padframe extends frame 
{ 
  private menubar mb; 
  private menu menufile; 
  private menu menuedit; 
  private menuitem[] mifile; 
  private textarea ta; 
  final private frame frame = this; 
  /** 
   * the inner class 
   * message handle 
   */ 
  class eventexit implements actionlistener 
  { 
    public void actionperformed(actionevent e)
    { 
      system.exit(0); 
    } 
  } 
  class systemexit extends windowadapter 
  { 
    public void windowclosing(windowevent e)
    { 
      system.exit(0); 
    } 
  } 
  class eventmenuclose implements actionlistener
  { 
    public void actionperformed(actionevent e)
    { 
      ta.settext(null); 
    } 
  } 
  class eventopenfile implements actionlistener
  { 
    public void actionperformed(actionevent e) 
    { 
      //create the openfile dialog 
      filedialog dlg = new filedialog(frame,"open files",filedialog.load);
      dlg.show(); 
       
      string strpath; 
      if((strpath = dlg.getdirectory()) != null) 
      { 
        //get the full path of the selected file
        strpath += dlg.getfile(); 
         
        //open the file 
        try 
        { 
          fileinputstream fis = new fileinputstream(strpath); 
          bufferedinputstream bis = new bufferedinputstream(fis); 
          byte[] buf = new byte[3000]; 
          int len = bis.read(buf); 
           
          ta.append(new string(buf,0,len)); 
          bis.close(); 
        } 
        catch(exception ex) 
        { 
          ex.printstacktrace(); 
        } 
      } 
    } 
  } 
  /** 
   * construction method 
   * adding menu and textarea components 
   * @param strtitle 
   */ 
  public padframe(string strtitle) 
  { 
    super(strtitle); 
    this.setlocation(400,200); 
    this.setsize(900, 630); 
     
    //create the menu bar 
    mb = new menubar(); 
    menufile = new menu("file"); 
    menuedit = new menu("edit"); 
    mifile = new menuitem[]{new menuitem("open"),new menuitem("close"),new menuitem("exit")}; 
    this.setmenubar(mb); 
    mb.add(menufile); 
    mb.add(menuedit); 
    for(int i = 0 ; i < mifile.length ; ++i) 
    { 
      menufile.add(mifile[i]); 
    } 
    //add event handle 
    setmenueventhandle(new eventexit(),"file",2); 
    setmenueventhandle(new eventopenfile(),"file",0); 
    setmenueventhandle(new eventmenuclose(),"file",1); 
    this.addwindowlistener(new systemexit()); 
     
    //add the textarea component 
    ta = new textarea(30,30); 
    this.add(ta); 
  } 
  public void setmenueventhandle(actionlistener al,string strmenu,int index) 
  { 
    if(strmenu == "file") 
    { 
      mifile[index].addactionlistener(al); 
    } 
  } 
  public int getmenuitemamount(string strmenu) 
  { 
    if("file" == strmenu) 
    { 
      return mifile.length; 
    } 
     
    return -1; 
  } 
  public static void main(string[] args) 
  { 
    padframe f = new padframe("notepad"); 
    f.show(); 
  } 
}

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