栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现的简易记事本

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现的简易记事本

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

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

JDK Version : 1.7.0

效果如下图所示:

源代码如下:

import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 
 
class Padframe extends frame 
{ 
  private MenuBar mb; 
  private Menu menuFile; 
  private Menu menuEdit; 
  private MenuItem[] miFile; 
  private textarea ta; 
  final private frame frame = this; 
   
  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(); 
 } 
      } 
    } 
  } 
   
  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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151651.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号