-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNotepad.java
535 lines (444 loc) · 13.5 KB
/
Notepad.java
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.RenderingHints.Key;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.undo.UndoManager;
public class Notepad extends JFrame implements ActionListener {
//窗体和输入区域
JPanel pl = new JPanel();
JTextArea myarea = new JTextArea();
private String filename; // 打开的文件名
String textContent = "";// 编辑框中的内容
UndoManager undoManager = new UndoManager();// 撤销管理器
public Notepad() {
initComponment();// 面板初始化
}
private void initComponment() {
// 菜单栏
JMenuBar mb = new JMenuBar();
// 弹出菜单
final JPopupMenu myPopMenu = new JPopupMenu();
JMenuItem copy_pop = new JMenuItem("复制");
JMenuItem cut_pop = new JMenuItem("剪切");
JMenuItem paste_pop = new JMenuItem("粘贴");
JMenuItem delete_pop = new JMenuItem("删除");
JMenuItem exit_pop = new JMenuItem("清空");
myPopMenu.add(cut_pop);
myPopMenu.add(copy_pop);
myPopMenu.add(delete_pop);
myPopMenu.add(paste_pop);
myPopMenu.add(exit_pop);
// 绑定监听器
cut_pop.addActionListener(this);
copy_pop.addActionListener(this);
delete_pop.addActionListener(this);
paste_pop.addActionListener(this);
exit_pop.addActionListener(this);
// 菜单
JMenu file = new JMenu("文件");
JMenu edit = new JMenu("编辑");
JMenu about = new JMenu("关于");
// 子菜单
JMenuItem new_file = new JMenuItem("新建");
JMenuItem open = new JMenuItem("打开");
JMenuItem save = new JMenuItem("保存");
JMenuItem save_as = new JMenuItem("另存为");
JMenuItem exit = new JMenuItem("退出");
JMenuItem copy = new JMenuItem("复制");
JMenuItem cut = new JMenuItem("剪切");
JMenuItem paste = new JMenuItem("粘贴");
JMenuItem delete = new JMenuItem("删除");
JMenuItem search = new JMenuItem("查找和替换");
JMenuItem aboutsoft = new JMenuItem("关于软件");
// 绑定监听事件
aboutsoft.addActionListener(this);
new_file.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
save_as.addActionListener(this);
exit.addActionListener(this);
copy.addActionListener(this);
cut.addActionListener(this);
paste.addActionListener(this);
delete.addActionListener(this);
search.addActionListener(this);
// 将菜单和相应的子菜单添加到菜单栏
mb.add(file);
mb.add(edit);
mb.add(about);
file.add(open);
file.add(new_file);
file.add(save);
file.add(save_as);
file.add(exit);
edit.add(copy);
edit.add(cut);
edit.add(paste);
edit.add(delete);
edit.add(search);
about.add(aboutsoft);
// 给文本区域添加滚动条
myarea.add(myPopMenu);
JScrollPane scrollpane = new JScrollPane(myarea);
add(scrollpane);
// 主窗口
setTitle("记事本");
setSize(600, 400);
setLocation(400, 300);
// 添加菜单栏
setJMenuBar(mb);
// 窗口监听
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
if(!myarea.getText().equals(textContent))
{
int result = JOptionPane.showConfirmDialog(null, "文件内容已改变,确认保存退出吗?", "警告", JOptionPane.YES_NO_OPTION);
switch (result) {
case JOptionPane.NO_OPTION:
System.exit(0);
break;
case JOptionPane.YES_OPTION:
save();
System.exit(0);
break;
default:
break;
}
}
else {
System.exit(0);
}
}
});
//键盘监听
myarea.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
//ctrl+f实现查找功能
if ((ke.getKeyCode() == KeyEvent.VK_F)
&& (ke.isControlDown()))
{
// 查找对话框
JDialog search = new JDialog(Notepad.this, "查找和替换");
search.setSize(200, 100);
search.setLocation(450, 350);
JLabel label_1 = new JLabel("查找的内容");
JLabel label_2 = new JLabel("替换的内容");
final JTextField textField_1 = new JTextField(5);
final JTextField textField_2 = new JTextField(5);
JButton buttonFind = new JButton("查找");
JButton buttonChange = new JButton("替换");
JPanel panel = new JPanel(new GridLayout(2, 3));
panel.add(label_1);
panel.add(textField_1);
panel.add(buttonFind);
panel.add(label_2);
panel.add(textField_2);
panel.add(buttonChange);
search.add(panel);
search.setVisible(true);
// 为查找下一个 按钮绑定监听事件
buttonFind.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String findText = textField_1.getText();// 查找的字符串
String textArea = myarea.getText();// 当前文本框的内容
start = textArea.indexOf(findText, end);
end = start + findText.length();
if (start == -1)// 没有找到
{
JOptionPane.showMessageDialog(null, "“"+findText+"”"+"已经查找完毕", "记事本", JOptionPane.WARNING_MESSAGE);
myarea.select(start, end);
} else {
myarea.select(start, end);
}
}
});
// 为替换按钮绑定监听时间
buttonChange.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String changeText = textField_2.getText();// 替换的字符串
myarea.select(start, end);
myarea.replaceSelection(changeText);
myarea.select(start, end);
}
});
}
//esc退出
if (ke.getKeyCode()==KeyEvent.VK_ESCAPE)
{
if(!myarea.getText().equals(textContent))
{
int result = JOptionPane.showConfirmDialog(null, "文件内容已改变,确认保存退出吗?", "警告", 1);
switch (result) {
case JOptionPane.NO_OPTION:
System.exit(0);
break;
case JOptionPane.YES_OPTION:
save();
System.exit(0);
break;
case JOptionPane.CANCEL_OPTION:
break;
default:
break;
}
}
}
}
});
// 鼠标监听
myarea.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int mods = e.getModifiers();
// 鼠标右键
if ((mods & InputEvent.BUTTON3_MASK) != 0) {
// 弹出菜单
myPopMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
}
// 相关变量
int start = 0;// 查找开始位置
int end = 0;// 查找结束位置
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
if (e.getActionCommand() == "新建") {
myarea.setText("");
} else if (e.getActionCommand() == "打开") {
FileDialog fileDialog = new FileDialog(this, "打开文件", FileDialog.LOAD);
fileDialog.setFile("*.txt");
fileDialog.setVisible(true);
if (fileDialog.getFile() != null) {
filename = fileDialog.getDirectory() + fileDialog.getFile();// 获得文件名
// 读取文件
FileReader file_reader = new FileReader(filename);// 此处必须要捕获异常
BufferedReader br = new BufferedReader(file_reader);
String temp = "";
while (br.ready())// 判断缓冲区是否为空,非空时返回true
{
int c = br.read();
temp = temp+ (char)c;
}
myarea.setText(temp);
br.close();
file_reader.close();
textContent = myarea.getText();
setTitle("记事本-" + filename);
}
} else if (e.getActionCommand() == "保存") {
save();
} else if (e.getActionCommand() == "另存为") {
otherSave();
} else if (e.getActionCommand() == "退出") {
if(!myarea.getText().equals(textContent))
{
int result = JOptionPane.showConfirmDialog(null, "文件内容已改变,确认保存退出吗?", "警告", 1);
switch (result) {
case JOptionPane.NO_OPTION:
System.exit(0);
break;
case JOptionPane.YES_OPTION:
save();
System.exit(0);
break;
case JOptionPane.CANCEL_OPTION:
break;
default:
break;
}
}
else {
System.exit(0);
}
} else if (e.getActionCommand() == "查找和替换") {
// 查找对话框
JDialog search = new JDialog(this, "查找和替换");
search.setSize(200, 100);
search.setLocation(450, 350);
JLabel label_1 = new JLabel("查找的内容");
JLabel label_2 = new JLabel("替换的内容");
final JTextField textField_1 = new JTextField(5);
final JTextField textField_2 = new JTextField(5);
JButton buttonFind = new JButton("查找");
JButton buttonChange = new JButton("替换");
JPanel panel = new JPanel(new GridLayout(2, 3));
panel.add(label_1);
panel.add(textField_1);
panel.add(buttonFind);
panel.add(label_2);
panel.add(textField_2);
panel.add(buttonChange);
search.add(panel);
search.setVisible(true);
// 为查找下一个 按钮绑定监听事件
buttonFind.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String findText = textField_1.getText();// 查找的字符串
String textArea = myarea.getText();// 当前文本框的内容
start = textArea.indexOf(findText, end);
end = start + findText.length();
// 没有找到
if (start == -1)
{
JOptionPane.showMessageDialog(null, "“"+findText+"”"+"已经查找完毕", "记事本", JOptionPane.WARNING_MESSAGE);
myarea.select(start, end);
} else {
myarea.select(start, end);
}
}
});
// 为替换按钮绑定监听事件
buttonChange.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String changeText = textField_2.getText();// 替换的字符串
myarea.select(start, end);
myarea.replaceSelection(changeText);
myarea.select(start, end);
}
});
} else if (e.getActionCommand() == "复制") {
copy();
} else if (e.getActionCommand() == "粘贴") {
paste();
} else if (e.getActionCommand() == "剪切") {
cut();
} else if (e.getActionCommand() == "删除") {
delete();
} else if (e.getActionCommand() == "关于软件") {
JOptionPane.showMessageDialog(null,"单金伟设计编写","软件信息",JOptionPane. INFORMATION_MESSAGE);
}else if (e.getActionCommand() == "清空")
{
int result = JOptionPane.showConfirmDialog(null, "确认清空所有文字吗?", "警告", 1);
if (result == JOptionPane.OK_OPTION) {
// myarea.replaceRange(null,0,textContent.length());
myarea.setText(null);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
//保存
private void save() {
if (filename != null)
{
try {
File file = new File(filename);
FileWriter file_writer = new FileWriter(file);
//将文件输出流包装进缓冲区
BufferedWriter bw = new BufferedWriter(file_writer);
PrintWriter pw = new PrintWriter(bw);
pw.print(myarea.getText());
textContent = myarea.getText();
pw.close();
bw.close();
file_writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}else {
otherSave();
}
}
//另存为
private void otherSave()
{
FileDialog fileDialog = new FileDialog(this, "另存为", FileDialog.SAVE);
fileDialog.setFile("*.txt");
fileDialog.setVisible(true);
if (fileDialog.getFile() != null) {
// 写入文件
FileWriter fw;
try {
fw = new FileWriter(fileDialog.getDirectory() + fileDialog.getFile());
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.print(myarea.getText());
textContent = myarea.getText();
pw.close();
bw.close();
fw.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//剪切
private void cut() {
copy();
delete();
}
//复制
private void copy() {
if (myarea.getSelectedText() == null) {
JOptionPane.showMessageDialog(null, "你没有选中任何文字!", "记事本", JOptionPane.WARNING_MESSAGE);
}
Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection(myarea.getSelectedText());
clipBoard.setContents(stringSelection, null);
}
//粘贴
private void paste() throws UnsupportedFlavorException, IOException {
String content_copy = "";
// 构造系统剪切板
Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
// 获取剪切板内容
Transferable content = clipBoard.getContents(null);
if (content != null) {
// 检查是否是文本类型
if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
content_copy = (String) content.getTransferData(DataFlavor.stringFlavor);
// 判断文本框中有无文字选中
if (myarea.getSelectedText() != null) {
myarea.replaceSelection(content_copy);
} else {
myarea.insert(content_copy, myarea.getSelectionStart());
}
}
}
}
//删除
private void delete() {
if (myarea.getSelectedText() == null) {
JOptionPane.showMessageDialog(null, "你没有选中任何文字!", "记事本", JOptionPane.WARNING_MESSAGE);
}
myarea.replaceSelection("");
}
public static void main(String[] args) {
new Notepad().setVisible(true);
}
}