-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotepad.java
172 lines (125 loc) · 5.28 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
package Projects;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.filechooser.*;
public class Notepad extends JFrame implements ActionListener{
JTextArea area;
JScrollPane pane;
String text;
Notepad(){
setBounds(0,0,1950,1050);
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem newdoc = new JMenuItem("New");
newdoc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
newdoc.addActionListener(this);
JMenuItem open = new JMenuItem("Open");
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
open.addActionListener(this);
JMenuItem save = new JMenuItem("Save");
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
save.addActionListener(this);
JMenuItem print = new JMenuItem("Print");
print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
print.addActionListener(this);
JMenuItem exit = new JMenuItem("Exit");
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
exit.addActionListener(this);
file.add(newdoc);
file.add(open);
file.add(save);
file.add(print);
file.add(exit);
JMenu edit = new JMenu("edit");
JMenuItem copy = new JMenuItem("Copy");
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
copy.addActionListener(this);
JMenuItem paste = new JMenuItem("Paste");
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
paste.addActionListener(this);
JMenuItem cut = new JMenuItem("Cut");
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));
cut.addActionListener(this);
JMenuItem selectall = new JMenuItem("Select All");
selectall.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
selectall.addActionListener(this);
edit.add(copy);
edit.add(paste);
edit.add(cut);
edit.add(selectall);
JMenu help = new JMenu("help");
JMenuItem about = new JMenuItem("About the Notepad");
about.addActionListener(this);
help.add(about);
menubar.add(file);
menubar.add(edit);
menubar.add(help);
setJMenuBar(menubar);
area = new JTextArea();
area.setFont(new Font("SAN_SERIF",Font.PLAIN,20));
area.setLineWrap(true);
area.setWrapStyleWord(true);
pane = new JScrollPane(area);
pane.setBorder(BorderFactory.createEmptyBorder());
add(pane,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae){
if (ae.getActionCommand().equals("New")){
area.setText("");
}else if(ae.getActionCommand().equals("Open")){
JFileChooser chooser = new JFileChooser("C:\\Users\\maleg\\Documents");
chooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter restrict = new FileNameExtensionFilter("Only .txt files", "txt");
chooser.addChoosableFileFilter(restrict);
int action =chooser.showOpenDialog(this);
if (action != JFileChooser.APPROVE_OPTION){
return;
}
File file = chooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
area.read(reader,null);
}catch (Exception e){
}
}else if(ae.getActionCommand().equals("Save")){
JFileChooser saveas = new JFileChooser();
saveas.setApproveButtonText("Save");
int action =saveas.showOpenDialog(this);
if (action != JFileChooser.APPROVE_OPTION){
return;
}
File filename = new File(saveas.getSelectedFile() +".txt");
BufferedWriter outFile = null;
try {
outFile = new BufferedWriter(new FileWriter(filename));
area.write(outFile);
} catch (Exception e) {
}
}else if(ae.getActionCommand().equals("Print")){
try {
area.print();
}catch (Exception e){}
}else if(ae.getActionCommand().equals("Exit")){
System.exit(0);
}else if (ae.getActionCommand().equals("Copy")){
text = area.getSelectedText();
}else if(ae.getActionCommand().equals("Paste")){
area.insert(text, area.getCaretPosition());
}else if(ae.getActionCommand().equals("Cut")){
text = area.getSelectedText();
area.replaceRange("",area.getSelectionStart(),area.getSelectionEnd());
}else if(ae.getActionCommand().equals("SelectAll")){
area.selectAll();
}else if(ae.getActionCommand().equals("About the Notepad")){
new About().setVisible(true);
}
}
public static void main(String[] args) {
new Notepad().setVisible(true);
}
}