-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notepad.java
109 lines (94 loc) · 3.37 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Notepad extends JFrame {
private JTextArea textArea;
private JFileChooser fileChooser;
public Notepad() {
super("Notepad");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea();
textArea.setFont(new Font("Comic Sans MS", Font.BOLD, 22));
add(new JScrollPane(textArea), BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem newFileItem = new JMenuItem("New");
newFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});
fileMenu.add(newFileItem);
JMenuItem openFileItem = new JMenuItem("Open...");
openFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
});
fileMenu.add(openFileItem);
JMenuItem saveFileItem = new JMenuItem("Save...");
saveFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
fileMenu.add(saveFileItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(exitItem);
fileChooser = new JFileChooser();
}
private void openFile() {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
textArea.setText(readFile(file));
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error reading file: " + e.getMessage());
}
}
}
private void saveFile() {
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
writeFile(file, textArea.getText());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Error writing file: " + e.getMessage());
}
}
}
private String readFile(File file) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
private void writeFile(File file, String text) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(text);
writer.close();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Notepad notepad = new Notepad();
notepad.setVisible(true);
}
});
}
}