设为首页 加入收藏

TOP

Java - 简易记事本(一)
2014-11-24 12:02:28 来源: 作者: 【 】 浏览:78
Tags:Java 简易 记事本
JDK Version : 1.7.0

\

\\

Source Code :
[java]
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)
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇实现通用的保存记录的方法 下一篇关于解析javax.persistence.Table..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: