Java实现的简易记事本

所属分类: 软件编程 / java 阅读数: 39
收藏 0 赞 0 分享

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

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

JDK Version : 1.7.0

效果如下图所示:

源代码如下:

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程序设计有所帮助。

更多精彩内容其他人还在看

java 中maven pom.xml文件教程详解

这篇文章主要介绍了java 中maven pom.xml文件教程详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合netty的实现方法

这篇文章主要介绍了spring boot整合netty的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Netty与Spring Boot的整合实现

这篇文章主要介绍了Netty与Spring Boot的整合的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring动态加载bean后调用实现方法解析

这篇文章主要介绍了Spring动态加载bean后调用实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现画图板上画一条直线

这篇文章主要为大家详细介绍了java实现画图板上画一条直线,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java通过python命令执行DataX任务的实例

今天小编就为大家分享一篇Java通过python命令执行DataX任务的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

springBoot集成redis的key,value序列化的相关问题

这篇文章主要介绍了springBoot集成redis的key,value序列化的相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java实现登录案例

这篇文章主要为大家详细介绍了java实现登录案例的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java解决请求跨域的两种方法

这篇文章主要为大家详细介绍了java解决请求跨域的两种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringBoot集成Beetl后统一处理页面异常的方法

这篇文章主要介绍了SpringBoot集成Beetl后统一处理页面异常的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多