JAVA 运用流编程实现简单的"记事本"功能

一、概要

1.功能介绍

2.实现的思路及步骤代码

3.完整代码

二、功能

运用IO流和Swing实现简单的记事本功能(打开、保存、退出)

三、思路及实现步骤

1.在构造函数中画出操作界面

 1 //创建jta
 2         jta = new JTextArea();
 3         jmb = new JMenuBar();
 4         jml = new JMenu("菜单(M)");
 5         //设置助记符
 6         jml.setMnemonic('M');
 7 
 8         //打开按钮
 9         jmi1 = new JMenuItem("打开", new ImageIcon("edit.gif"));
10         //添加图标的第二种方法
11         //ImageIcon ic = new ImageIcon("edit.gif");
12         //jmi1.setIcon(ic);
13         //保存按钮
14         jmi2 = new JMenuItem("保存");
15         //退出按钮
16         jmi3 = new JMenuItem("退出");
17 
18         //放入控件
19         this.setJMenuBar(jmb);
20         //把JMenu放入到JMenuBar
21         jmb.add(jml);
22         //把item放入到Menu中去
23         jml.add(jmi1);
24         jml.add(jmi2);
25         jml.add(jmi3);
26 
27         //放入到JFrame里
28         this.add(jta);
29         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
30         this.setSize(400, 300);
31         this.setVisible(true);
View Code

界面展示:

 

2.注册监听事件(判断点了哪个按钮)

 1 //进行注册监听
 2         //打开
 3         jmi1.addActionListener(this);
 4         jmi1.setActionCommand("open");
 5         //保存
 6         jmi2.addActionListener(this);
 7         jmi2.setActionCommand("save");
 8         //退出
 9         jmi3.addActionListener(this);
10         jmi3.setActionCommand("quit");
View Code

3.根据事件反馈判断要进行的操作(根据点击的按钮来判断要做什么事)

①打开

 1 if (e.getActionCommand().equals("open")) {
 2             //JFileChooser文件选择组件
 3             JFileChooser jfc1 = new JFileChooser();
 4             //设置名字
 5             jfc1.setDialogTitle("请选择文件...");
 6 
 7             jfc1.showOpenDialog(null);
 8             //显示
 9             jfc1.setVisible(true);
10 
11             String file = null;
12             try {
13                 //得到用户选择的文件绝对(全)路径
14                 file = jfc1.getSelectedFile().getAbsolutePath();
15 
16                 //System.out.println(filename);
17                 FileReader fr = null;
18                 BufferedReader br = null;
19                 try {
20                     fr = new FileReader(file);
21                     br = new BufferedReader(fr);
22                     //从文件中读取信息并显示到jta
23                     String s = "";
24                     String allCon = "";
25                     while ((s = br.readLine()) != null) {
26                         allCon += s + "\r\n";
27                     }
28 
29                     //放置到jta即可
30                     jta.setText(allCon);
31 
32                 } catch (Exception ex) {
33                     ex.printStackTrace();
34                 } finally {
35                     try {
36                         if (br != null && fr != null) {
37                             br.close();
38                             fr.close();
39                         }
40                     } catch (Exception ex) {
41                         ex.printStackTrace();
42                     }
43                 }
44             } catch (Exception ex) {
45                 System.out.println("未选中文件");
46                 //ex.printStackTrace();
47             }
48         }
View Code

②保存

 1 if (e.getActionCommand().equals("save")) {
 2             //出现保存对话框
 3             JFileChooser jfc = new JFileChooser();
 4             jfc.setDialogTitle("另存为...");
 5             //按默认的方式显示
 6             jfc.showSaveDialog(null);
 7             jfc.setVisible(true);
 8 
 9             String file = null;
10             try {
11                 //得到用户希望把文件保存到的地址(文件绝对路径)
12                 file = jfc.getSelectedFile().getAbsolutePath();
13 
14                 //写入到指定文件
15                 FileWriter fw = null;
16                 BufferedWriter bw = null;
17                 try {
18                     fw = new FileWriter(file);
19                     bw = new BufferedWriter(fw);
20 
21                     bw.write(this.jta.getText());
22                 } catch (Exception ex) {
23                     ex.printStackTrace();
24                 } finally {
25                     try {
26                         //bw和fw的关闭顺序不能写反,否则会报错
27                         if (bw != null && fw != null) {
28                             bw.close();
29                             fw.close();
30                         }
31                     } catch (Exception ex) {
32                         ex.printStackTrace();
33                     }
34                 }
35             } catch (Exception ex) {
36                 System.out.println("未选中文件");
37                 //ex.printStackTrace();
38                 //System.out.println(ex.getMessage());
39             }
40         }
View Code

③退出

1 if (e.getActionCommand().equals("quit")) {
2             System.exit(0);
3         }
View Code

四、附上完整代码

  1 /**
  2  * 我的记事本(界面+功能)
  3  */
  4 package com.test3;
  5 
  6 import javax.swing.*;
  7 import java.awt.event.*;
  8 import java.io.BufferedReader;
  9 import java.io.BufferedWriter;
 10 import java.io.FileReader;
 11 import java.io.FileWriter;
 12 
 13 public class NotePad extends JFrame implements ActionListener {
 14     //定义需要的组件
 15     JTextArea jta = null;
 16 
 17     //菜单条
 18     JMenuBar jmb = null;
 19 
 20     //定义JMenu(菜单栏按钮)
 21     JMenu jml = null;
 22 
 23     //定义JMenuItem(功能按钮)
 24     JMenuItem jmi1 = null;
 25     JMenuItem jmi2 = null;
 26     JMenuItem jmi3 = null;
 27 
 28     public static void main(String[] args) {
 29         NotePad notePad = new NotePad();
 30     }
 31 
 32     //构造函数
 33     public NotePad() {
 34         //创建jta
 35         jta = new JTextArea();
 36         jmb = new JMenuBar();
 37         jml = new JMenu("菜单(M)");
 38         //设置助记符
 39         jml.setMnemonic('M');
 40 
 41         //打开按钮
 42         jmi1 = new JMenuItem("打开", new ImageIcon("edit.gif"));
 43         //添加图标的第二种方法
 44         //ImageIcon ic = new ImageIcon("edit.gif");
 45         //jmi1.setIcon(ic);
 46         //保存按钮
 47         jmi2 = new JMenuItem("保存");
 48         //退出按钮
 49         jmi3 = new JMenuItem("退出");
 50 
 51         Listen();
 52 
 53         //放入控件
 54         this.setJMenuBar(jmb);
 55         //把JMenu放入到JMenuBar
 56         jmb.add(jml);
 57         //把item放入到Menu中去
 58         jml.add(jmi1);
 59         jml.add(jmi2);
 60         jml.add(jmi3);
 61 
 62         //放入到JFrame里
 63         this.add(jta);
 64         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 65         this.setSize(400, 300);
 66         this.setVisible(true);
 67     }
 68 
 69     //监听事件
 70     public void Listen()
 71     {
 72         //进行注册监听
 73         //打开
 74         jmi1.addActionListener(this);
 75         jmi1.setActionCommand("open");
 76         //保存
 77         jmi2.addActionListener(this);
 78         jmi2.setActionCommand("save");
 79         //退出
 80         jmi3.addActionListener(this);
 81         jmi3.setActionCommand("quit");
 82     }
 83 
 84     @Override
 85     public void actionPerformed(ActionEvent e) {
 86         //判断触发了哪个功能按钮
 87         //打开
 88         if (e.getActionCommand().equals("open")) {
 89             //JFileChooser文件选择组件
 90             JFileChooser jfc1 = new JFileChooser();
 91             //设置名字
 92             jfc1.setDialogTitle("请选择文件...");
 93 
 94             jfc1.showOpenDialog(null);
 95             //显示
 96             jfc1.setVisible(true);
 97 
 98             String file = null;
 99             try {
100                 //得到用户选择的文件绝对(全)路径
101                 file = jfc1.getSelectedFile().getAbsolutePath();
102 
103                 //System.out.println(filename);
104                 FileReader fr = null;
105                 BufferedReader br = null;
106                 try {
107                     fr = new FileReader(file);
108                     br = new BufferedReader(fr);
109                     //从文件中读取信息并显示到jta
110                     String s = "";
111                     String allCon = "";
112                     while ((s = br.readLine()) != null) {
113                         allCon += s + "\r\n";
114                     }
115 
116                     //放置到jta即可
117                     jta.setText(allCon);
118 
119                 } catch (Exception ex) {
120                     ex.printStackTrace();
121                 } finally {
122                     try {
123                         if (br != null && fr != null) {
124                             br.close();
125                             fr.close();
126                         }
127                     } catch (Exception ex) {
128                         ex.printStackTrace();
129                     }
130                 }
131             } catch (Exception ex) {
132                 System.out.println("未选中文件");
133                 //ex.printStackTrace();
134             }
135         }
136         //保存
137         else if (e.getActionCommand().equals("save")) {
138             //出现保存对话框
139             JFileChooser jfc = new JFileChooser();
140             jfc.setDialogTitle("另存为...");
141             //按默认的方式显示
142             jfc.showSaveDialog(null);
143             jfc.setVisible(true);
144 
145             String file = null;
146             try {
147                 //得到用户希望把文件保存到的地址(文件绝对路径)
148                 file = jfc.getSelectedFile().getAbsolutePath();
149 
150                 //写入到指定文件
151                 FileWriter fw = null;
152                 BufferedWriter bw = null;
153                 try {
154                     fw = new FileWriter(file);
155                     bw = new BufferedWriter(fw);
156 
157                     bw.write(this.jta.getText());
158                 } catch (Exception ex) {
159                     ex.printStackTrace();
160                 } finally {
161                     try {
162                         //bw和fw的关闭顺序不能写反,否则会报错
163                         if (bw != null && fw != null) {
164                             bw.close();
165                             fw.close();
166                         }
167                     } catch (Exception ex) {
168                         ex.printStackTrace();
169                     }
170                 }
171             } catch (Exception ex) {
172                 System.out.println("未选中文件");
173                 //ex.printStackTrace();
174                 //System.out.println(ex.getMessage());
175             }
176         }
177         //退出
178         else if (e.getActionCommand().equals("quit")) {
179             System.exit(0);
180         }
181     }
182 }
View Code

猜你喜欢

转载自www.cnblogs.com/ShaneLiu/p/10836519.html