-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
257 lines (202 loc) · 6.88 KB
/
GUI.java
File metadata and controls
257 lines (202 loc) · 6.88 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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoManager;
public class GUI implements ActionListener {
JFrame window;
// Text Area
JTextArea textArea;
JScrollPane scrollPane;
boolean wordWrapOn = false;
// Top Menu Bar
JMenuBar menuBar;
JMenu menuFile, menuEdit, menuFormat, menuColor;
// File menu
JMenuItem iNew, iOpen, iSave, iSaveAs, iExit;
// Edit Menu
JMenuItem iUndo, iRedo;
// Format Menu
JMenuItem iWrap, iFontArial, iFontCSMS, iFontTNR, iFontSize8, iFontSize12, iFontSize16, iFontSize20, iFontSize24, iFontSize28;
JMenu menuFont, menuFontSize;
// COLOR MENU
JMenuItem iColor1, iColor2, iColor3;
Function_File file = new Function_File(this);
Function_Format format = new Function_Format(this);
Function_Color color = new Function_Color(this);
Function_Edit edit = new Function_Edit(this);
Key_Handler kHandler = new Key_Handler(this);
UndoManager um = new UndoManager();
public static void main(String[] args) {
new GUI();
}
public GUI() {
createWindow();
createTextArea();
createMenuBar();
createFileMenu();
createEditMenu();
createFormatMenu();
createColorMenu();
format.selectedFont = "Arial";
format.createFont(16);
format.wordWrap();
color.changeColor("White");
window.setVisible(true);
}
public void createWindow() {
window = new JFrame("Notepad");
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void createTextArea() {
textArea = new JTextArea();
textArea.addKeyListener(kHandler);
textArea.getDocument().addUndoableEditListener(
new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
um.addEdit(e.getEdit());
}
});
scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBorder(BorderFactory.createEmptyBorder());
window.add(scrollPane);
}
public void createMenuBar() {
menuBar = new JMenuBar();
window.setJMenuBar(menuBar);
menuFile = new JMenu("File");
menuBar.add(menuFile);
menuEdit = new JMenu("Edit");
menuBar.add(menuEdit);
menuFormat = new JMenu("Format");
menuBar.add(menuFormat);
menuColor = new JMenu("Color");
menuBar.add(menuColor);
}
public void createFileMenu() {
iNew = new JMenuItem("New");
iNew.addActionListener(this);
iNew.setActionCommand("New");
menuFile.add(iNew);
iOpen = new JMenuItem("Open");
iOpen.addActionListener(this);
iOpen.setActionCommand("Open");
menuFile.add(iOpen);
iSave = new JMenuItem("Save");
iSave.addActionListener(this);
iSave.setActionCommand("Save");
menuFile.add(iSave);
iSaveAs = new JMenuItem("Save As");
iSaveAs.addActionListener(this);
iSaveAs.setActionCommand("SaveAs");
menuFile.add(iSaveAs);
iExit = new JMenuItem("Exit");
iExit.addActionListener(this);
iExit.setActionCommand("Exit");
menuFile.add(iExit);
}
public void createEditMenu() {
iUndo = new JMenuItem("Undo");
iUndo.addActionListener(this);
iUndo.setActionCommand("Undo");
menuEdit.add(iUndo);
iRedo = new JMenuItem("Redo");
iRedo.addActionListener(this);
iRedo.setActionCommand("Redo");
menuEdit.add(iRedo);
}
public void createFormatMenu() {
iWrap = new JMenuItem("Word Wrap: Off");
iWrap.addActionListener(this);
iWrap.setActionCommand("Word Wrap");
menuFormat.add(iWrap);
menuFont = new JMenu("Font");
menuFont.add(menuFont);
iFontArial = new JMenuItem("Arial");
iFontArial.addActionListener(this);
iFontArial.setActionCommand("Arial");
menuFont.add(iFontArial);
iFontCSMS = new JMenuItem("Comic Sans MS");
iFontCSMS.addActionListener(this);
iFontCSMS.setActionCommand("Comic Sans MS");
menuFont.add(iFontCSMS);
iFontTNR = new JMenuItem("Times New Roman");
iFontTNR.addActionListener(this);
iFontTNR.setActionCommand("Times New Roman");
menuFont.add(iFontTNR);
menuFontSize = new JMenu("Font Size");
menuFormat.add(menuFontSize);
iFontSize8 = new JMenuItem("8");
iFontSize8.addActionListener(this);
iFontSize8.setActionCommand("size8");
menuFontSize.add(iFontSize8);
iFontSize12 = new JMenuItem("12");
iFontSize12.addActionListener(this);
iFontSize12.setActionCommand("size12");
menuFontSize.add(iFontSize12);
iFontSize16 = new JMenuItem("16");
iFontSize16.addActionListener(this);
iFontSize16.setActionCommand("size16");
menuFontSize.add(iFontSize16);
iFontSize20 = new JMenuItem("20");
iFontSize20.addActionListener(this);
iFontSize20.setActionCommand("size20");
menuFontSize.add(iFontSize20);
iFontSize24 = new JMenuItem("24");
iFontSize24.addActionListener(this);
iFontSize24.setActionCommand("size24");
menuFontSize.add(iFontSize24);
iFontSize28 = new JMenuItem("28");
iFontSize28.addActionListener(this);
iFontSize28.setActionCommand("size28");
menuFontSize.add(iFontSize28);
}
public void createColorMenu() {
iColor1 = new JMenuItem("White");
iColor1.addActionListener(this);
iColor1.setActionCommand("Whilte");
menuColor.add(iColor1);
iColor2 = new JMenuItem("Black");
iColor2.addActionListener(this);
iColor2.setActionCommand("Black");
menuColor.add(iColor2);
iColor3 = new JMenuItem("Blue");
iColor3.addActionListener(this);
iColor3.setActionCommand("Blue");
menuColor.add(iColor3);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch(command) {
case "New": file.newFile(); break;
case "Open": file.open(); break;
case "Save": file.save(); break;
case "SaveAs": file.saveAs(); break;
case "Exit": file.exit(); break;
case "Undo": edit.undo(); break;
case "Redo": edit.redo(); break;
case "Word Wrap": format.wordWrap(); break;
case "Arial": format.setFont(command); break;
case "Comic Sans MS": format.setFont(command); break;
case "Times New Roman": format.setFont(command); break;
case "size8": format.createFont(8); break;
case "size12": format.createFont(12); break;
case "size16": format.createFont(16); break;
case "size20": format.createFont(20); break;
case "size24": format.createFont(24); break;
case "size28": format.createFont(28); break;
case "White": color.changeColor(command); break;
case "Black": color.changeColor(command); break;
case "Blue": color.changeColor(command); break;
}
}
}