-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesApp.java
More file actions
75 lines (66 loc) · 2.18 KB
/
NotesApp.java
File metadata and controls
75 lines (66 loc) · 2.18 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.time.*;
import java.io.*;
public class NotesApp extends Main{
private JFrame frame;
private JTextArea notesTextArea;
private JScrollPane scrollPane;
private JButton saveButton;
public NotesApp() {
// create the frame
frame = new JFrame("Notes App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
// create the text area and add it to a scroll pane
notesTextArea = new JTextArea();
scrollPane = new JScrollPane(notesTextArea);
// create the save button
saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveNotes();
}
});
// add the components to the frame
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(saveButton, BorderLayout.SOUTH);
// show the frame
frame.setVisible(true);
}
// save the notes to a file
private void saveNotes() {
LocalTime CurrentTime = LocalTime.now();
String TextInside = notesTextArea.getText();
boolean isImportant = TextInside.charAt(0)=='*';
if(isImportant){
TextInside = TextInside.substring(1);
}
//Add note to the ArrayList
new Notes(CurrentTime,TextInside,isImportant);
frame.setVisible(false);
frame.dispose();
try {
File myObj = new File("notes.txt");
if (myObj.createNewFile()) {
//System.out.println("File created: " + myObj.getName());
} else {
//System.out.println("File already exists.");
}
} catch (IOException e) {
//System.out.println("An error occurred12.");
e.printStackTrace();
}
try {
FileWriter myWriter = new FileWriter("notes.txt",true);
myWriter.write(TextInside+" "+CurrentTime.toString()+"\n");
myWriter.close();
//System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
//System.out.println("An error occurred.");
e.printStackTrace();
}
}
}