-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotepad.java
171 lines (138 loc) · 6.04 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
package notepad;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
import java.io.*;
public class Notepad extends JFrame implements ActionListener {
JTextArea area;
String text;
Notepad(){
setTitle("Notepad");
ImageIcon notepadIcon=new ImageIcon(ClassLoader.getSystemResource("notepad/icons/notepad.png"));
Image icon=notepadIcon.getImage();
setIconImage(icon);
JMenuBar menubar = new JMenuBar();
menubar.setBackground(Color.WHITE);
JMenu file = new JMenu("File");
file.setFont(new Font("AERIAL",Font.PLAIN,14));
JMenuItem newdoc = new JMenuItem("NEW");
newdoc.addActionListener(this);
newdoc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));
JMenuItem open = new JMenuItem("Open");
open.addActionListener(this);
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
JMenuItem save = new JMenuItem("Save");
save.addActionListener(this);
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
JMenuItem print = new JMenuItem("Print");
print.addActionListener(this);
print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,ActionEvent.CTRL_MASK));
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(this);
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,ActionEvent.CTRL_MASK));
file.add(newdoc);
file.add(open);
file.add(save);
file.add(print);
file.add(exit);
menubar.add(file);
JMenu edit = new JMenu("Edit");
edit.setFont(new Font("AERIAL",Font.PLAIN,14));
JMenuItem copy = new JMenuItem("Copy");
copy.addActionListener(this);
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));
JMenuItem paste = new JMenuItem("Paste");
paste.addActionListener(this);
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));
JMenuItem cut = new JMenuItem("Cut");
cut.addActionListener(this);
cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));
JMenuItem selectAll = new JMenuItem("Selectall");
selectAll.addActionListener(this);
selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK));
edit.add(copy);
edit.add(paste);
edit.add(cut);
edit.add(selectAll);
menubar.add(edit);
JMenu helpmenu = new JMenu("Help");
helpmenu.setFont(new Font("AERIAL",Font.PLAIN,14));
JMenuItem help = new JMenuItem("About");
help.addActionListener(this);
help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,ActionEvent.CTRL_MASK));
helpmenu.add(help);
menubar.add(helpmenu);
setJMenuBar(menubar);
area =new JTextArea();
area.setFont(new Font("SAN_SERIF",Font.PLAIN,18));
area.setLineWrap(true);
area.setWrapStyleWord(true);
JScrollPane pane =new JScrollPane(area);
pane.setBorder(BorderFactory.createEmptyBorder());
add(pane);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand().equals("New")){
area.setText(" ");
}else if(ae.getActionCommand().equals("Open")){
JFileChooser chooser = new JFileChooser();
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){
e.printStackTrace();
}
}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){
e.printStackTrace();
}
} else if(ae.getActionCommand().equals("Print")){
try{
area.print();
} catch (Exception e){
e.printStackTrace();
}
} 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("Select All")){
area.selectAll();
}
else if(ae.getActionCommand().equals("About")){
new About().setVisible(true);
}
}
public static void main(String[] args) {
new Notepad();
}
}