一个简易的记事本
来源:互联网 发布:美国大发战争财 数据 编辑:程序博客网 时间:2024/04/29 14:50
可以通过菜单选项打开文本文件,可以编辑,保存文件
packagemymenu;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
classMenuDemo2{
private Frame f = new Frame();
private MenuBar mbar = new MenuBar();
private Menu fileMenu = new Menu("文件");
private FileDialog openDialog = newFileDialog(f,"打开文件",FileDialog.LOAD);
private FileDialog saveDialog = newFileDialog(f,"保存文件",FileDialog.SAVE);
private MenuItem openItem = newMenuItem("打开");
private MenuItem saveItem = newMenuItem("保存");
private MenuItem closeItem = newMenuItem("关闭");
private TextArea textArea = newTextArea(30,30);
private File file = null;
MenuDemo2(){
init();
event();
}
public void init(){
f.setBounds(100,100,500,400);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
mbar.add(fileMenu);
f.setMenuBar(mbar);
f.setVisible(true);
f.add(textArea);
//f.pack();
}
public void event(){
f.addWindowListener(newWindowAdapter(){
public voidwindowClosing(WindowEvent e){
System.exit(0);
}
});
//在关闭条目上添加一个监听器,如果点击则退出
closeItem.addActionListener(newActionListener(){
public voidactionPerformed(ActionEvent e){
System.exit(0);
}
});
//保存文件
saveItem.addActionListener(newActionListener(){
public voidactionPerformed(ActionEvent e){
if(file ==null)
saveDialog.setVisible(true);
StringfilePath = saveDialog.getDirectory();
StringfileName = saveDialog.getFile();
if(filePath== null || fileName == null)
return;
BufferedWriterbw = null;
file = newFile(filePath,fileName );
try{
bw =new BufferedWriter(new FileWriter(file));
Stringstr = textArea.getText();
bw.write(str);
bw.flush();
}catch(IOExceptionex){
thrownew RuntimeException("文件保存失败");
}finally{
if(bw!= null){
try{
bw.close();
}catch(IOExceptionex){
throw new RuntimeException("输入流关闭失败");
}
}
}
}
});
//打开文件
openItem.addActionListener(newActionListener(){
public voidactionPerformed(ActionEvent e){
openDialog.setVisible(true);
StringfilePath = openDialog.getDirectory();
StringfileName = openDialog.getFile();
if(filePath== null || fileName == null)
return;
textArea.setText("");
file = newFile(filePath,fileName );
BufferedReaderbr = null;
try{
br = new BufferedReader(newFileReader(file));
Stringstr = null;
while((str= br.readLine()) != null){
textArea.append(str+ "\r\n");
}
}catch(IOExceptionex){
thrownew RuntimeException("文件打开失败");
}finally{
if(br!= null){
try{
br.close();
}catch(IOExceptionex){
throw new RuntimeException("输出流关闭失败");
}
}
}
}
});
}
public static void main(String[] args){
new MenuDemo2();
}
}
- 一个简易的记事本
- 用java写的一个简易记事本
- 【Java】实现一个简易记事本Notepad的编写
- 通过JAVA程序写一个简易的记事本
- MinGW写的简易记事本
- QT我的简易记事本
- 简易记事本
- 简易记事本
- 简易记事本
- C#第二次作业:用c#语言写一个简易的记事本和图片随机显示
- 一个简单的记事本
- 一个记事本的源程序
- 记事本的一个BUG
- 一个简单的记事本
- 一个简单的记事本
- 记事本的一个bug
- MinGW写的简易记事本 Ver0.02
- 用Java写的简易记事本
- VIM 高亮设置
- redmine安装
- 《操作系统概念-第七版》概念复习笔记第4章
- 随笔一记
- Linux下Clock skew detected的解决办法 && Linux时间戳和标准时间的互转
- 一个简易的记事本
- jdk1.5新特性(-)
- struts2中异常处理
- c8800 mp4设置
- C++ switch
- 反射(reflect)
- 黑马程序员 - Java - 环境变量
- hashCode()方法的作用
- 详解Centos与ubuntu下的useradd与adduser