本帖最后由 黑马_位志国 于 2013-3-26 08:24 编辑
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyMenuTest {
private Frame frame;
private MenuBar fileMenuBar;
private Menu fileMenu;
private MenuItem openMenuItem, saveMenuItem, closeMenuItem;
private FileDialog openFileDialog, saveFileDialog;
private TextArea textArea;
private File file;
public MyMenuTest() {
init();
}
private void init() {
// TODO Auto-generated method stub
//创建窗体,并设置窗体的位置及其大小
frame = new Frame("my frame");
frame.setBounds(200, 100, 800, 600);
//创建菜单条、菜单和菜单项
fileMenuBar = new MenuBar();
fileMenu = new Menu("文件");
openMenuItem = new MenuItem("打开");
saveMenuItem = new MenuItem("保存");
closeMenuItem = new MenuItem("退出");
//将菜单条、菜单和菜单项添加到frame窗体中
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(closeMenuItem);
fileMenuBar.add(fileMenu);
frame.setMenuBar(fileMenuBar);
//创建打开和保存文件的对话框
openFileDialog = new FileDialog(frame, "打开", FileDialog.LOAD);
saveFileDialog = new FileDialog(frame, "保存", FileDialog.SAVE);
//添加事件处理机制
myEvent();
//创建文本区,并添加到frame窗体中
textArea = new TextArea();
frame.add(textArea);
//显示frame窗体
frame.setVisible(true);
}
private void myEvent(){
openMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
openFileDialog.setVisible(true);
//获取选中文件的路径及其文件名
String dirPath = openFileDialog.getDirectory();
String fileName = openFileDialog.getFile();
if((dirPath != null) || (fileName != null))
return;
textArea.setText("");
File file = new File(dirPath, fileName);
//将文件内容显示在frame窗体中的文本区中
BufferedReader bufr = null;
try {
bufr = new BufferedReader(new FileReader(file));
String line = null;
while((line = bufr.readLine()) != null){
textArea.append(line + "\n\r");
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
throw new RuntimeException("文件打开失败!");
} catch (IOException e1) {
// TODO Auto-generated catch block
throw new RuntimeException("文件读取失败!");
}finally{
if(bufr != null){
try {
bufr.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
throw new RuntimeException("资源关闭失败!");
}
}
}
}
});
saveMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(file == null){
saveFileDialog.setVisible(true);
//获取保存文件的路径及其文件名
String dirPath = saveFileDialog.getDirectory();
String fileName = saveFileDialog.getFile();
if(dirPath != null || fileName != null)
return;
file = new File(dirPath, fileName);
//将frame窗体中的文本区内容保存在内存中
BufferedWriter bufw = null;
try {
bufw = new BufferedWriter(new FileWriter(file));
String text = textArea.getText();
bufw.write(text);
bufw.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
throw new RuntimeException("文件保存失败!");
}finally{
if(bufw != null){
try {
bufw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
throw new RuntimeException("资源关闭失败!");
}
}
}
}
}
});
//对窗体关闭动作进行监听,并处理
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
//对退出菜单项进行监听,并处理
closeMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyMenuTest();
}
}
此程序是我看着视屏写的,为什么不可以在我计算机上读取文件和保存文件?
求解中......
希望有朋友能尽快把我解决,谢谢啦! |