Java實(shí)現(xiàn)的簡(jiǎn)易記事本
本文實(shí)例講述了Java實(shí)現(xiàn)的簡(jiǎn)易記事本。分享給大家供大家參考。具體如下:
感覺(jué)這個(gè)沒(méi)有自己以前用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();
}
}
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring?Boot?集成?Quartz并使用Cron?表達(dá)式實(shí)現(xiàn)定時(shí)任務(wù)
本篇文章介紹了如何在?Spring?Boot?中集成?Quartz?進(jìn)行定時(shí)任務(wù)調(diào)度,并通過(guò)?Cron?表達(dá)式?控制任務(wù)執(zhí)行時(shí)間,Quartz?提供了更強(qiáng)大的任務(wù)調(diào)度能力,比?@Scheduled?注解更靈活,適用于復(fù)雜的定時(shí)任務(wù)需求2025-04-04
用Java集合中的Collections.sort方法如何對(duì)list排序(兩種方法)
本文通過(guò)兩種方法給大家介紹java集合中的Collections.sort方法對(duì)list排序,第一種方式是list中的對(duì)象實(shí)現(xiàn)Comparable接口,第二種方法是根據(jù)Collections.sort重載方法實(shí)現(xiàn),對(duì)collections.sort方法感興趣的朋友一起學(xué)習(xí)吧2015-10-10
關(guān)于java.lang.IncompatibleClassChangeError錯(cuò)誤解決方案
最近開發(fā)中遇到類沖突報(bào)錯(cuò) java.lang.IncompatibleClassChangeError,所以下面這篇文章主要給大家介紹了關(guān)于java.lang.IncompatibleClassChangeError錯(cuò)誤的解決方案,需要的朋友可以參考下2024-02-02
SpringBoot定時(shí)任務(wù)設(shè)計(jì)之時(shí)間輪案例原理詳解
這篇文章主要為大家介紹了SpringBoot定時(shí)任務(wù)設(shè)計(jì)之時(shí)間輪案例原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
你所不知道的Spring的@Autowired實(shí)現(xiàn)細(xì)節(jié)分析
這篇文章主要介紹了你所不知道的Spring的@Autowired實(shí)現(xiàn)細(xì)節(jié)分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
MPAndroidChart開源圖表庫(kù)的使用介紹之餅狀圖、折線圖和柱狀圖
這篇文章主要介紹了MPAndroidChart開源圖表庫(kù)的使用介紹之餅狀圖、折線圖和柱狀圖的相關(guān)資料,需要的朋友可以參考下2016-02-02

