实现打开与保存的记事本
1 packagecom.tanzhou.notepad.util;2
3 importjava.awt.Color;4 importjava.awt.event.ActionEvent;5 importjava.awt.event.ActionListener;6 importjava.io.BufferedReader;7 importjava.io.BufferedWriter;8 importjava.io.FileReader;9 importjava.io.FileWriter;10 importjava.io.IOException;11
12 importjavax.swing.JFileChooser;13 importjavax.swing.JFrame;14 importjavax.swing.JMenu;15 importjavax.swing.JMenuBar;16 importjavax.swing.JMenuItem;17 importjavax.swing.JTextArea;18
19 /**
20 * @className NotepadTest.java21 *22 * @Description:TODO(生成一个记事本)23 *24 * @date 2018.7.2325 *26 *@author银桑的洞爷湖27 *28 */
29 public class NotepadTest extends JFrame implements ActionListener{// 监听事件
30 // 1.定义一个文本框 全局变量,在哪都可以使用 new的话一开始就会占用空间;null的话,创造了但不会占用空间31 JTextArea jTextArea=null;32 // 2.定义一个菜单栏33 JMenuBar jMenuBar = null;34 // 3.定义一个菜单"文件"35 JMenu jMenu1 = null;36 // 4.定义 打开和37 JMenuItem jMenuItem1 = null;38 // 5.保存39 JMenuItem jMenuItem2 = null;40 // 6.定义一个文件选择41 JFileChooser jFileChooser = null;42 // 7.定义一个filereader 文件输入流:从中读取数据,打开时要用输入流读取43 FileReader fileReader = null;44 // 8.定义一个filewrite 文件输出流:保存 保存到某个文件中45 FileWriter fileWriter = null;46 // 9.定义一个缓冲字符输入流 缓冲区 可当成一个水池,每次给里面放一瓢水,等这个水池蓄满以后一次性取出来47 BufferedReader bufferedReader = null;48 // 10.定义一个缓冲字符输出流49 BufferedWriter bufferedWrider = null;50
51 /**
52 * @Description:TODO(构造函数) 方法名必须和类名相同53 *@param
54 *@return思路:55 生成一个记事本56 1.定义记事本的样式57 2.生成一个记事本58 3.实现记事本的功能 打开和保存59 */
60
61 publicNotepadTest(){62 // 1.实例化文本框 new63 jTextArea=newJTextArea();64 // 2.实例化菜单栏65 jMenuBar=newJMenuBar();66 // 3.实例化一个菜单"文件"67 jMenu1=new JMenu("文件");68 // 4.实例化 打开 按钮69 jMenuItem1=new JMenuItem("打开");70 // 绑定监听事件 监视者的意思71 jMenuItem1.addActionListener(this);// this表当前对象 jMenuItem172 jMenuItem1.setActionCommand("打开");// setActionCommand:给哪个设置监听事件
73 // 5.实例化 保存 按钮74 jMenuItem2 = new JMenuItem("保存");75 // 绑定监听事件 监视者的意思76 jMenuItem2.addActionListener(this);//this表当前对象
77 jMenuItem2.setActionCommand("保存");//setActionCommand:给哪个设置监听事件78 //6.设置文本框的背景颜色
79 jTextArea.setBackground(Color.WHITE);80 // 7.归为 定位
81 // 将菜单栏添加到窗体中82 this.setJMenuBar( jMenuBar );83 // 将菜单添加到菜单栏中84 jMenuBar.add(jMenu1);85 // 将“打开”和“保存”添加到“文件”菜单中86 jMenu1.add(jMenuItem1);87 jMenu1.add(jMenuItem2);88 // 将文本框添加到窗体中89 this.add(jTextArea);90 // 8.设置记事本标题91 this.setTitle("啊灵呀记事本");92 // 9.设置记事本大小93 this.setSize(800,600);94 // 10.当我们关闭窗口时 关闭进程95 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);96 // 11.显示窗口97 this.setVisible(true);98 }99
100 //用流一般要抛异常
101
102 /**
103 * @Description:TODO(main函数 门)104 *@paramargs105 *@returnvoid106 */
107
108 public static voidmain(String[] args){109 NotepadTest notepadTest=newNotepadTest();110 }111
112 @Override113 public voidactionPerformed(ActionEvent e){114 // 实现 打开 和 保存 功能
//点 打开 按钮115 if(e.getActionCommand().equals("打开")){116 // 1.实例化jFileChooser 文件选择117 jFileChooser=newJFileChooser();118 // 2.定义一个标题119 jFileChooser.setDialogTitle("选择要打开的文件");120 // 3.设置一个默认的路径121 jFileChooser.showOpenDialog(null);122 // 4.当点击打开时,让文件选择框自动弹出123 jFileChooser.setVisible(true);124 //5.用address 保存用户编辑文件的绝对路径
125 String address=jFileChooser.getSelectedFile().getAbsolutePath();126
127 try{128 //6.实例化文件输入流
129 fileReader=newFileReader(address);130 //7.实例化缓冲字符输入流 缓冲区 暂时存放进去
131 bufferedReader =newBufferedReader(fileReader);132 //8.定义一个str判断输入的字符是否已经为空
133 String str="";134 //9.定义一个strAll接收文件的全部信息
135 String strAll="";136 //去缓冲区取数据并保存到strAll中 read()返回-1,读取完 readLine():一行行读
137 while((str=bufferedReader.readLine())!=null){138 strAll=str+"\r\n";139 }140 jTextArea.setText(strAll);141 }catch(IOException e1){142 e1.printStackTrace();143 }finally{// 可保证里面的代码一定会执行144 try{145 bufferedReader.close();146 fileReader.close();147 }catch(IOException e1){148 e1.printStackTrace();149 }150 }151 }
//点 保存 按钮
if(e.getActionCommand().equals("保存")){
JFileChooser jFileChooser1=new JFileChooser(); jFileChooser1.setDialogTitle("保存");
jFileChooser1.showSaveDialog(null);