//模拟windows的文本编辑器
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.io.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class TextFileEditorJFrame extends JFrame implements ActionListener
{
private File file;
//当前读取的文件
private JTextArea text;
//输入文本的文本区
private JFileChooser fchooser;
//选择文件的对话框
//private JOptionPane optionpane;
public
TextFileEditorJFrame()
{
super("文本文件编辑器");//调用JFrame的构造函数,是窗口的标题为文本编辑器
this.setBounds(800, 500, 800, 500);//设置窗口的大小
this.setDefaultCloseOperation
(HIDE_ON_CLOSE);//设置用户在此窗体上发起 "close" 时默认执行的操作
//HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册的 WindowListener 对象后自动隐藏该窗体
this.text=new JTextArea();
//this.text.setName("新建本文一");
this.getContentPane().add(new JScrollPane(this.text));//返回此窗体的对象,在对象中加入文本编辑区,设置滚动条
JMenuBar menubar=new JMenuBar();//添加菜单栏
this.setJMenuBar(menubar);
String menustr[]={"文件","编辑","插入","格式","工具","帮助"};
JMenu menu[]=new JMenu[menustr.length];
for(int i=0;i
{
menu[i]=new JMenu(menustr[i]);
menubar.add(menu[i]);
}
String menuitemstr[]={"新建","打开","保存","另存为"};
JMenuItem menuitem[]=new JMenuItem[menuitemstr.length];//创造jmenuitem对象数组
//菜单中的项的实现。菜单项本质上是位于列表中的按钮。当用户选择“按钮”时,则执行与菜单项关联的操作。
for(int i=0;i
{
menuitem[i]=new JMenuItem(menuitemstr[i]);//给每个对象初始化
menu[0].add(menuitem[i]);//链接到主菜单上
menuitem[i].addActionListener(this);//添加动作事件
}
//设置打开和保存的图标
menuitem[1].setIcon(new ImageIcon("Test8_2_open.png"));
menuitem[2].setIcon(new ImageIcon("Test8_2_save.png"));
JToolBar toolbar=new JToolBar();//JToolBar 提供了一个用来显示常用的 Action 或控件的组件
this.getContentPane().add(toolbar, "North");
//
JButton bopen=new JButton("打开",new ImageIcon("Test8_2_open.png"));
//
bopen.addActionListener(this);
//
toolbar.add(bopen);
//
JButton bsave=new JButton("保存",new ImageIcon("Test8_3_save.png"));
//
bsave.addActionListener(this);
//
toolbar.add(bsave);
this.setVisible(true);//根据参数 b 的值显示或隐藏此 Window.true显示,false隐藏
//this.file=null;
this.fchooser=new JFileChooser(new File(".",""));//文件选择对话框,初始路径为当前路径
//this.optionpane=new JOptionPane()
FileNameExtensionFilter filefilter = new FileNameExtensionFilter("文本文件(.txt)","txt");
this.fchooser.setFileFilter(filefilter);
//FileFilter f = (FileFilter) new
FileExtensionFilter("test","test");
}
//
public
TextFileEditorJFrame(File file)
//
{
//
this();
//
if(file!=null)
//
{
//
this.file=file;
//
this.text.setText(this.readFromFile());
//
this.setTitle(this.file.getName());
//
}
//
}
//
public TextFileEditorJFrame(String filename)
//
{
//
this(new File(filename));
//
}
public void writeToFile(String lines)
{
try
{
FileWriter fout=new FileWriter(this.file);//创建字符输出流对象
fout.write(lines+"\r\n");
fout.close();
}
catch(IOException ioex)
{
JOptionPane.showMessageDialog(this, "有IO错,写入"+file.getName()+"文件不成功");
}
}
public String readFromFile()//使用流从当前文件中读出字符串
{
char lines[]=null;
try
{
FileReader fin=new FileReader(this.file);
lines=new char[(int)this.file.length()];
fin.read(lines);
fin.close();
}
catch(FileNotFoundException fe)
{
JOptionPane.showMessageDialog(this, "IO错,文件"+this.file.getName()+"不存在");
}
catch(IOException ioex)
{
JOptionPane.showMessageDialog(this, "IO错,读取文件"+this.file.getName()+"不成功");
}
finally
{
return new String(lines);
}
}
public void actionPerformed(ActionEvent e)//实现事件处理接口
{
if(e.getActionCommand()=="新建")
//新建文件时,新建新的文件
{
this.file=null;
this.setTitle("未命名");
this.text.setText("");
}
if(e.getActionCommand()=="打开"&&this.fchooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
//显示打开文件对话框,并且单击打开按钮
{
this.file=this.fchooser.getSelectedFile();//
this.setTitle(this.file.getName());
this.text.setText(this.readFromFile());
}
if(e.getActionCommand()=="保存"&&this.file!=null)//保存已经存在的非空文件
{
//this.file=this.fchooser.getSelectedFile();//
this.setTitle(this.file.getName());
this.text.setText(this.readFromFile());
this.writeToFile(this.text.getText());
JOptionPane.showMessageDialog(this, "保存成功");
System.out.println(this.file.getAbsolutePath());
}
//
if(e.getActionCommand()=="保存"&&this.text.getText()==" ")
//
JOptionPane.showMessageDialog(this, "请先新建文件再保存");
if(e.getActionCommand()=="保存"&&this.file==null&&this.fchooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION||e.getActionCommand()=="另存为"&&this.fchooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
//保存空文件,执行"另存为"菜单时显示保存文件对话框且点击保存按钮时
{
this.file=this.fchooser.getSelectedFile();
if(!this.file.getName().endsWith(".txt"))//不是以txt结尾自动加上txt
this.file=new File(file.getAbsolutePath()+".txt");
this.writeToFile(this.text.getText());
//this.setTitle("新建文本一");
//System.out.println(this.getTitle()+"11111");
//System.out.println(this.text.getText()+"22222");
JOptionPane.showMessageDialog(this, "保存成功");
// System.out.println(this.file.getAbsolutePath());
}
}
public static void
main(String arg[])
{
TextFileEditorJFrame a=new
TextFileEditorJFrame();
}
}