Java實現的簡易記事本_java

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.