Java 记事本

使用Java语言编写一个记事本应用,涉及到Swing界面编程,文件读写等内容。

知识点:

  • Java Swing界面编程
  • 文件读写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.test.fileeditor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class FileEditor extends JFrame {
private JTextField selectField; //定义一个私有的文件的绝对路径文本域对象
private JTextArea editArea; //定义一个私有的编辑区对象
private JButton saveBtn; //定义一个私有的“保存”按钮对象
private JButton openFileBtn; //定义一个私有的“浏览”按钮对象

//定义一个私有的记录目录层次数,其初始值为0

private int level = 0;
public FileEditor(){
this.init();
}
private void init() {
//设置标题为 Editor
this.setTitle("Editor");
this.setBounds(300,50,600,650); //设置组件的大小
//创建一个选择框对象
selectField = new JTextField(40);
//创建一个按钮对象
openFileBtn = new JButton("Browse");
//为刚创建的按钮添加监听事件
openFileBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
FileEditor.this.level = 0;
String path = selectField.getText();
// 浏览目录或者文件
openDirOrFile(path.replaceAll("//", "\\\\"));

//String的replaceAll()方法,是采用正则表达式规则去匹配的。
// 参数中的“//”在java语言中被解析为“/”,而“\\\\”在java语言中被解析成“\\”,还要经正则表达式转换为“\”。
}


});

//新建一个流布局,并且左对齐的面板
JPanel upPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
upPanel.setBackground(Color.cyan); //设置画板的背景颜色

upPanel.add(selectField); //将选择框加入画板中
upPanel.add(openFileBtn); //将按钮加入画板中
this.add(upPanel, BorderLayout.NORTH); //将面板放在北边区域

/*
* 创建文本编辑区,并加入到整个布局的中间区域
*/
editArea = new JTextArea();
ScrollPane scollPanel = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
scollPanel.add(editArea);
this.add(scollPanel, BorderLayout.CENTER);
/*
* 创建保存按钮,并为按钮添加监听事件
*/
saveBtn = new JButton("save");
saveBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
saveFile(); // 保存
}

});
JPanel southPanel = new JPanel();
southPanel.setBackground(Color.green);
southPanel.add(saveBtn);
this.add(southPanel, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

}
/*
* 保存文件
*/
private void saveFile() {
FileDialog fd = new FileDialog(this,"Save File");
//设置需要保存文件的后缀
fd.setFile("untitled.txt");
//设置为“保存”模式
fd.setMode(FileDialog.SAVE);

fd.setVisible(true);
//获取文件名
String fileName = fd.getFile();
//获取对话框的当前目录
String dir = fd.getDirectory();
//根据目录名、文件名创建一个文件,即要保存的目标文件
File newFile = new File(dir+File.separator+fileName);
PrintWriter pw = null;
try{
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(newFile)));

String str = editArea.getText();
pw.println(str);
pw.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
pw.close();
}
}
/*
* 打开目录或文件
*/
private void openDirOrFile(String absolutePath) {
//absolutePath:指定目录或文件的绝对路径名
File file = new File(absolutePath);
//判断文件或目录是否存在
if(!(file.exists())){
editArea.setText("The file does not exist!");
//判断是否是一个目录
}else if(file.isDirectory()){
editArea.setText(null);
showDir(file);
//判断是否是一个文件
}else if(file.isFile()){
try{
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String str = null;
editArea.setText(null);
while((str = br.readLine())!=null){
editArea.append(str+"\r\n");
}
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
/*
* 浏览目录,建立树形图
*/
private void showDir(File directory){
File[] files = directory.listFiles();
int len = files.length;
for(int i=0;i<len;i++){
if(files[i].isDirectory()){
for(int j=0;j<this.level;j++){
editArea.append(" ");
}
editArea.append("|--"+files[i].getName()+"(Folder)\r\n");
this.level++;
showDir(files[i]);
this.level--;
}
}
}
/*
* 测试,运行整个程序测试效果
*/
public static void main(String[] args){
new FileEditor();
}
}
如果觉得对您有帮助,就扫我交个朋友吧!