您的位置:首页 > 其它

怎么让文件的内容在textArea中显示出来?

2009-05-05 20:20 381 查看
package javanotpad;

import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class MenuFrame extends JFrame implements ActionListener
{
private static final long serialVersionUID = 201269579650631599L;
private String filePath;

@SuppressWarnings("static-access")
public void initialize()
{
initMenuBar();
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setTitle("Java ");// 设置窗口标题
this.setSize(600, 700);// 设置窗口大小
this.setLocationRelativeTo(null);// 设置窗口位置
this.setVisible(true);// 设置窗口可见性
this.add(new TextArea());// 文本编辑区域
}

@SuppressWarnings("static-access")
public void initMenuBar()
{
JMenuBar menuBar = new JMenuBar();
final JFileChooser fileChoose = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("*.txt", "all file");

// fileChoose.addChoosableFileFilter(filter);

JMenu file, edit, view, search, document, tool, window, help;
JMenuItem fileItem1, fileItem2, fileItem3, fileItem4, fileItem5, editItem1, editItem2, editItem3, helpItem1;

file = new JMenu("文件(F)");// 文件菜单
edit = new JMenu("编辑(E)");// 编辑菜单
view = new JMenu("视图(V)");// 视图菜单
search = new JMenu("搜索(S)");// 上搜菜单
document = new JMenu("文档(D)");// 文档菜单
tool = new JMenu("工具(T)");// 工具菜单
window = new JMenu("窗口(W)");// 窗口菜单
help = new JMenu("帮助(H)");// 帮助菜单

// 文件菜单项
fileItem1 = new JMenuItem("新建");
fileItem2 = new JMenuItem("打开");
fileItem3 = new JMenuItem("保存");
fileItem4 = new JMenuItem("另存为");
fileItem5 = new JMenuItem("退出");

file.add(fileItem1);// 添加文件子菜单项,新建
file.add(fileItem2);// 添加文件子菜单项,打开
file.add(fileItem3);// 添加文件子菜单项,保存
file.add(fileItem4);// 添加文件子菜单项,另存为
file.add(fileItem5);// 添加文件子菜单项,退出

// 编辑菜单项
editItem1 = new JMenuItem("剪切");
editItem2 = new JMenuItem("粘贴");
editItem3 = new JMenuItem("复制");
edit.add(editItem1);// 添加编辑子菜单项,剪切
edit.add(editItem2);// 添加编辑子菜单项,粘贴
edit.add(editItem3);// 添加编辑子菜单项,复制

// 帮助菜单项
helpItem1 = new JMenuItem("关于");
help.add(helpItem1);// 添加帮助子菜单项,关于
helpItem1.addActionListener(this);// 添加关于按钮的事件

menuBar.add(file);// 添加文件菜单
menuBar.add(edit);// 添加编辑菜单
menuBar.add(view);// 添加视图菜单
menuBar.add(search);// 添加搜索菜单
menuBar.add(document);// 添加文档长度
menuBar.add(tool);// 添加工具菜单
menuBar.add(window);// 添加窗口
menuBar.add(help);// 添加帮助菜单
this.setJMenuBar(menuBar);// 添加菜单栏

fileChoose.setFileFilter(filter);// 设置打开文件的类型

fileItem2.addActionListener(new ActionListener()// 添加打开文件对话框事件
{
@Override
public void actionPerformed(ActionEvent e)
{
int returnVal = fileChoose
.showOpenDialog(MenuFrame.this);
if (returnVal == fileChoose.APPROVE_OPTION)
{
filePath = fileChoose.getSelectedFile()
.getAbsolutePath();
try
{
readFile(filePath);
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
}
});

fileItem3.addActionListener(new ActionListener()// 添加保存文件对话框
{
@Override
public void actionPerformed(ActionEvent e)
{
int returnVal = fileChoose
.showSaveDialog(MenuFrame.this);
if (returnVal == fileChoose.APPROVE_OPTION)
{
filePath = fileChoose.getSelectedFile()
.getAbsolutePath();
try
{
writerFile(filePath);
} catch (IOException e1)
{
e1.printStackTrace();
}
} else
{
JOptionPane.showMessageDialog(null,"文件将默认保存在:/r/n"+filePath,
"提示",JOptionPane.INFORMATION_MESSAGE);
}
}
});

fileItem5.addActionListener(new ActionListener()// 添加退出程序事件
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}

@Override
public void actionPerformed(ActionEvent e)// 关于菜单项信息
{
JOptionPane.showMessageDialog(null, "2009-5-4 11:01:29", "关于",
JOptionPane.INFORMATION_MESSAGE);
// JOptionPane.showInternalMessageDialog(null, "2009年5月5日17:22:47", "关于",
// JOptionPane.INFORMATION_MESSAGE);
}

void readFile(String path) throws FileNotFoundException// 显示打开文件的内容
{
File f = new File(path);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s;
try
{
s = br.readLine();
while (s != null)
{
s += s;
System.out.println(s);
s = br.readLine();
}

} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (br != null)
{
br.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
try
{
if (fr != null)
{
fr.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}

void writerFile(String path) throws IOException// 保存文件内容
{
// File file = new File(path);
// FileWriter fw = new FileWriter(file);
// BufferedWriter bw = new BufferedWriter(fw);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐