-
Notifications
You must be signed in to change notification settings - Fork 5
/
Editor.java
203 lines (167 loc) · 4.67 KB
/
Editor.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
package fgfdhds;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.plaf.metal.*;
import javax.swing.text.*;
class Editor extends JFrame implements ActionListener {
// Text component
JTextArea t;
// Frame
JFrame f;
// Constructor
Editor()
{
// Create a frame
f = new JFrame("editor");
try {
// Set metl look and feel
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
// Set theme to ocean
MetalLookAndFeel.setCurrentTheme(new OceanTheme());
}
catch (Exception e) {
}
// Text component
t = new JTextArea();
t.setBackground(Color.BLACK);
t.setFont(new Font("Times New Roman", Font.BOLD,30));
t.setForeground(Color.WHITE);
// Create a menubar
JMenuBar mb = new JMenuBar();
mb.setBackground(Color.GREEN);
// Create amenu for menu
JMenu m1 = new JMenu("File");
// Create menu items
JMenuItem mi1 = new JMenuItem("New");
JMenuItem mi2 = new JMenuItem("Open");
JMenuItem mi3 = new JMenuItem("Save");
JMenuItem mi9 = new JMenuItem("Print");
// Add action listener
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi9.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi9);
// Create amenu for menu
JMenu m2 = new JMenu("Edit");
// Create menu items
JMenuItem mi4 = new JMenuItem("cut");
JMenuItem mi5 = new JMenuItem("copy");
JMenuItem mi6 = new JMenuItem("paste");
// Add action listener
mi4.addActionListener(this);
mi5.addActionListener(this);
mi6.addActionListener(this);
m2.add(mi4);
m2.add(mi5);
m2.add(mi6);
JMenuItem mc = new JMenuItem("close");
mc.addActionListener(this);
mc.setBackground(Color.GREEN);
mb.add(m1);
mb.add(m2);
mb.add(mc);
f.setJMenuBar(mb);
f.add(t);
f.setSize(500, 500);
f.show();
}
// If a button is pressed
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("cut")) {
t.cut();
}
else if (s.equals("copy")) {
t.copy();
}
else if (s.equals("paste")) {
t.paste();
}
else if (s.equals("Save")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsSaveDialog function to show the save dialog
int r = j.showSaveDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File(j.getSelectedFile().getAbsolutePath());
try {
// Create a file writer
FileWriter wr = new FileWriter(fi, false);
// Create buffered writer to write
BufferedWriter w = new BufferedWriter(wr);
// Write
w.write(t.getText());
w.flush();
w.close();
}
catch (Exception evt) {
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
// If the user cancelled the operation
else
JOptionPane.showMessageDialog(f, "the user cancelled the operation");
}
else if (s.equals("Print")) {
try {
// print the file
t.print();
}
catch (Exception evt) {
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
else if (s.equals("Open")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsOpenDialog function to show the save dialog
int r = j.showOpenDialog(null);
// If the user selects a file
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File(j.getSelectedFile().getAbsolutePath());
try {
// String
String s1 = "", sl = "";
// File reader
FileReader fr = new FileReader(fi);
// Buffered reader
BufferedReader br = new BufferedReader(fr);
// Initilize sl
sl = br.readLine();
// Take the input from the file
while ((s1 = br.readLine()) != null) {
sl = sl + "\n" + s1;
}
// Set the text
t.setText(sl);
}
catch (Exception evt) {
JOptionPane.showMessageDialog(f, evt.getMessage());
}
}
// If the user cancelled the operation
else
JOptionPane.showMessageDialog(f, "the user cancelled the operation");
}
else if (s.equals("New")) {
t.setText("");
}
else if (s.equals("close")) {
f.setVisible(false);
}
}
// Main class
public static void main(String args[])
{
Editor e = new Editor();
}
}