Java實現(xiàn)的簡易記事本
更新時間:2015年04月16日 14:41:12 作者:司青
這篇文章主要介紹了Java實現(xiàn)的簡易記事本,較為詳細(xì)的分析了基于java實現(xiàn)記事本程序的完整過程,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Java實現(xiàn)的簡易記事本。分享給大家供大家參考。具體如下:
感覺這個沒有自己以前用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程序設(shè)計有所幫助。
相關(guān)文章
深入解析堆排序的算法思想及Java代碼的實現(xiàn)演示
堆排序基于二叉堆結(jié)構(gòu)即完全二叉樹,可利用最大堆和最小堆的組建方式來進(jìn)行排序,這里就來深入解析堆排序的算法思想及Java代碼的實現(xiàn)演示2016-06-06Java中的線程中斷機(jī)制和LockSupport詳解
這篇文章主要介紹了Java中的線程中斷機(jī)制和LockSupport詳解,在Java中沒有辦法立即停止一條線程,然而停止線程卻顯得尤為重要,如取消一個耗時操作,因此,Java提供了一種用于停止線程的協(xié)商機(jī)制中斷,也即中斷標(biāo)識協(xié)商機(jī)制,需要的朋友可以參考下2023-09-09微信、支付寶二碼合一掃碼支付實現(xiàn)思路(java)
這篇文章主要為大家詳細(xì)介紹了微信、支付寶二碼合一掃碼支付實現(xiàn)思路,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08SpringBoot基于HttpMessageConverter實現(xiàn)全局日期格式化
這篇文章主要介紹了SpringBoot基于HttpMessageConverter實現(xiàn)全局日期格式化,使用Jackson消息轉(zhuǎn)換器,非常具有實用價值,需要的朋友可以參考下2018-12-12Kotlin傳遞可變長參數(shù)給Java可變參數(shù)實例代碼
這篇文章主要介紹了Kotlin傳遞可變長參數(shù)給Java可變參數(shù)實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01