-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotepad.java
More file actions
323 lines (251 loc) · 7.59 KB
/
Notepad.java
File metadata and controls
323 lines (251 loc) · 7.59 KB
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
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
public class Notepad implements ActionListener{
JFrame jf;
JMenuBar menubar;
JMenu file, edit, format, help;
JMenuItem neww, open, save , saveas, exit, cut, copy, paste, selectall, fontcolor, bgcolor, choosefont;
JTextArea textarea;
JScrollPane scrollpane;
String default_title = "Untitled - Notepad";
File saveFile;
JButton ok, cancel;
JComboBox fontfamily, fontstyle, fontsize;
JFrame fontframe;
Notepad(){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}catch(Exception e){
System.out.println(e);
}
jf = new JFrame(default_title);
jf.setSize(1100,500);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(3);
menubar = new JMenuBar();
file = new JMenu("File");
neww = new JMenuItem("New Ctrl+N");
neww.addActionListener(this);
file.add(neww);
open = new JMenuItem("Open Ctrl+O");
open.addActionListener(this);
file.add(open);
save = new JMenuItem("Save Ctrl+S");
save.addActionListener(this);
file.add(save);
saveas = new JMenuItem("Save as Ctrl+Shift+S");
saveas.addActionListener(this);
file.add(saveas);
exit = new JMenuItem("Exit");
exit.addActionListener(this);
file.add(exit);
menubar.add(file);
edit = new JMenu("Edit");
cut = new JMenuItem("Cut Ctrl+X");
cut.addActionListener(this);
edit.add(cut);
copy = new JMenuItem("Copy Ctrl+C");
copy.addActionListener(this);
edit.add(copy);
paste = new JMenuItem("Paste Ctrl+V");
paste.addActionListener(this);
edit.add(paste);
selectall = new JMenuItem("Select All Ctrl+A");
selectall.addActionListener(this);
edit.add(selectall);
menubar.add(edit);
format = new JMenu("Format");
fontcolor = new JMenuItem("Font Color");
fontcolor.addActionListener(this);
format.add(fontcolor);
bgcolor = new JMenuItem("Background Color");
bgcolor.addActionListener(this);
format.add(bgcolor);
choosefont = new JMenuItem("Font");
choosefont.addActionListener(this);
format.add(choosefont);
menubar.add(format);
help = new JMenu("Help");
help.addActionListener(this);
menubar.add(help);
jf.setJMenuBar(menubar);
textarea = new JTextArea();
scrollpane = new JScrollPane(textarea);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jf.add(scrollpane);
jf.setVisible(true);
}
public static void main(String[] args) throws Exception{
new Notepad();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == neww) {
String title = jf.getTitle();
String text = textarea.getText();
if(!text.equals("")) {
int i = JOptionPane.showConfirmDialog(open, "Do you want to save this file ?");
if(i == 0) {
saveFile();
jf.setTitle(default_title);
textarea.setText("");
}else if(i == 1){
textarea.setText("");
}
}
}
if(e.getSource() == open) {
openFile();
}
if(e.getSource() == save) {
saveFile();
}
if(e.getSource() == saveas) {
saveAs();
}
if(e.getSource() == exit) {
System.exit(0);
}
if(e.getSource() == cut) {
textarea.cut();
}
if(e.getSource() == copy) {
textarea.copy();
}
if(e.getSource() == paste) {
textarea.paste();
}
if(e.getSource() == selectall) {
textarea.selectAll();
}
if(e.getSource() == fontcolor) {
setFontColor();
}
if(e.getSource() == bgcolor) {
setBackgroundColor();
}
if(e.getSource() == choosefont) {
openFontFrame();
}
if(e.getSource() == ok) {
setNewFontOnTextArea();
}
}
void openFile() {
JFileChooser filechooser = new JFileChooser();
int i = filechooser.showOpenDialog(jf);
if(i==0) {
File filee = filechooser.getSelectedFile();
try {
textarea.setText("");
FileInputStream fis = new FileInputStream(filee);
int ii;
while((ii = fis.read()) != -1) {
textarea.append(String.valueOf((char)ii));
}
jf.setTitle(filee.getName());
}
catch(Exception ee) {
System.out.println(ee);
}
}
}
void saveFile() {
String title = jf.getTitle();
if(title.equals(default_title)) {
saveAs();
}
else {
try {
String str = textarea.getText();
FileOutputStream fos = new FileOutputStream(saveFile);
fos.write(str.getBytes());
}catch(Exception e) {
System.out.println(e);
}
}
}
void saveAs() {
JFileChooser filechooser = new JFileChooser();
int i = filechooser.showOpenDialog(jf);
if(i==0) {
try {
saveFile = filechooser.getSelectedFile();
String str = textarea.getText();
FileOutputStream fos = new FileOutputStream(saveFile);
fos.write(str.getBytes());
jf.setTitle(saveFile.getName());
}catch(Exception e) {
System.out.println(e);
}
}
}
void setFontColor() {
Color c = JColorChooser.showDialog(jf, "Choose Font Color", Color.black);
textarea.setForeground(c);
}
void setBackgroundColor() {
Color c = JColorChooser.showDialog(open, "Choose Background Color", Color.white);
textarea.setBackground(c);
}
void openFontFrame() {
fontframe = new JFrame("Choose Font");
fontframe.setSize(600,400);
fontframe.setLocationRelativeTo(jf);
fontframe.setLayout(null);
String[] fontfamilyy = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
fontfamily = new JComboBox(fontfamilyy);
fontfamily.setBounds(50, 50, 200, 40);
fontframe.add(fontfamily);
String[] stylee = {"Plain", "Bold", "Italic"};
fontstyle = new JComboBox(stylee);
fontstyle.setBounds(300, 50, 100, 40);
fontframe.add(fontstyle);
String[] sizee = {"10","16","20","30","40","50","60"};
fontsize = new JComboBox(sizee);
fontsize.setBounds(450, 50, 70, 40);
fontframe.add(fontsize);
ok = new JButton("Ok");
ok.setBounds(100, 150, 80, 40);
ok.addActionListener(this);
fontframe.add(ok);
cancel = new JButton("Cancle");
cancel.setBounds(250, 150, 80, 40);
fontframe.add(cancel);
fontframe.setVisible(true);
}
void setNewFontOnTextArea() {
String ffamily = (String)fontfamily.getSelectedItem();
String fsize = (String)fontsize.getSelectedItem();
String fstyle = (String)fontstyle.getSelectedItem();
int fstylee = 0;
if(fstyle.equals("Plain")) {
fstylee = 0;
}else if(fstyle.equals("Bold")) {
fstylee = 1;
}else if(fstyle.equals("Italic")) {
fstylee = 2;
}
Font f = new Font(ffamily, fstylee, Integer.parseInt(fsize));
textarea.setFont(f);
fontframe.setVisible(false);
}
}