Java文本编辑器(测试版)

来源:互联网 发布:ftp端口被使用不能上网 编辑:程序博客网 时间:2024/04/30 01:56
//模拟windows的文本编辑器
import java.awt.event.*;
import javax.swing.*;
importjavax.swing.JOptionPane;
import java.io.*;
importjavax.swing.filechooser.FileNameExtensionFilter;


public class TextFileEditorJFrameextends JFrame implements ActionListener
{
   private File file;            //当前读取的文件
   private JTextArea text;        //输入文本的文本区
   private JFileChooser fchooser;  //选择文件的对话框
   //private JOptionPaneoptionpane; 
   
   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(newJScrollPane(this.text));//返回此窗体的对象,在对象中加入文本编辑区,设置滚动条
       JMenuBarmenubar=new JMenuBar();//添加菜单栏
      this.setJMenuBar(menubar);
       Stringmenustr[]={"文件","编辑","插入","格式","工具","帮助"};
       JMenumenu[]=new JMenu[menustr.length];
       for(inti=0;i
       {
          menu[i]=newJMenu(menustr[i]);
          menubar.add(menu[i]);
       }
       Stringmenuitemstr[]={"新建","打开","保存","另存为"};
       JMenuItemmenuitem[]=newJMenuItem[menuitemstr.length];//创造jmenuitem对象数组
     //菜单中的项的实现。菜单项本质上是位于列表中的按钮。当用户选择“按钮”时,则执行与菜单项关联的操作。
       for(inti=0;i
       {
          menuitem[i]=newJMenuItem(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"));
       JToolBartoolbar=new JToolBar();//JToolBar 提供了一个用来显示常用的 Action 或控件的组件
      this.getContentPane().add(toolbar, "North");
//       JButtonbopen=new JButton("打开",new ImageIcon("Test8_2_open.png"));
//      bopen.addActionListener(this);
//      toolbar.add(bopen);
//       JButtonbsave=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(newFile(".",""));//文件选择对话框,初始路径为当前路径
      //this.optionpane=new JOptionPane()
      FileNameExtensionFilter filefilter = newFileNameExtensionFilter("文本文件(.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(Stringfilename)
//   {
//       this(newFile(filename));
//   }
   public void writeToFile(String lines)
   {
       try
       {
          FileWriter fout=newFileWriter(this.file);//创建字符输出流对象
         fout.write(lines+"\r\n");
          fout.close();
       }
      catch(IOException ioex)
       {
         JOptionPane.showMessageDialog(this,"有IO错,写入"+file.getName()+"文件不成功");
       }
   }
   public StringreadFromFile()//使用流从当前文件中读出字符串
   {
       charlines[]=null;
       try
       {
          FileReader fin=newFileReader(this.file);
          lines=newchar[(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 newString(lines);
       }
   }
   public void actionPerformed(ActionEvente)//实现事件处理接口
   {
      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=newFile(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(Stringarg[])
   {
      TextFileEditorJFrame a=new TextFileEditorJFrame();       
   }    

0 0
原创粉丝点击