-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal1.java
More file actions
99 lines (88 loc) · 3.27 KB
/
Final1.java
File metadata and controls
99 lines (88 loc) · 3.27 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Final1 {
public static void main(String[] args) {
HashMap<String,String> frenchWords = new HashMap<String,String>();
JFrame window = new JFrame("Final Project");
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
JPanel middle = new JPanel();
middle.setLayout(new GridLayout(2,0));
JPanel bottom = new JPanel();
bottom.setLayout(new GridLayout(0,2));
JLabel englishLabel = new JLabel("English:");
JLabel foreignLabel = new JLabel("French:");
JTextField englishTF = new JTextField(); //set preferred size n stuff
middle.add(englishLabel);
middle.add(englishTF);
JTextField foreignTF = new JTextField();
middle.add(foreignLabel);
middle.add(foreignTF);
//add foreign script (e.g. cyrillic, hanzi) option
content.add(middle,BorderLayout.CENTER);
JButton saveButton = new JButton("Save");
bottom.add(saveButton);
JButton displayButton = new JButton("Display");
bottom.add(displayButton);
//add search, flashcard options later
content.add(bottom,BorderLayout.PAGE_END);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String eng = englishTF.getText();
String forn = foreignTF.getText();
//write to file
try {
FileWriter writer = new FileWriter("foreignWords.txt", true);
writer.write(eng + ":" + forn);
writer.write("\r\n"); // write new line
writer.close();
} catch (IOException i) {
i.printStackTrace();
}
}
});
String line;
try{
BufferedReader reader = new BufferedReader(new FileReader("foreignWords.txt"));
while ((line = reader.readLine()) != null)
{
String[] parts = line.split(":", 2);
if (parts.length >= 2)
{
String key = parts[0];
String value = parts[1];
frenchWords.put(key, value);
} else {
System.out.println("ignoring line: " + line);
}
}
} catch (Exception f){
englishTF.setText("File not found");
}
displayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame displayWindow = new JFrame();
JPanel displayContent = new JPanel();
displayContent.setLayout(new GridLayout(0,2)); //@TODO make a scrollList later
for (HashMap.Entry<String, String> entry : frenchWords.entrySet()) {
displayContent.add(new JLabel(entry.getKey().toString())); //prints the english word
displayContent.add(new JLabel(entry.getValue().toString())); //prints the corresponding french word
//@TODO alphabetize later
}
displayWindow.setContentPane(displayContent);
displayWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
displayWindow.setLocation(700,200);
displayWindow.setSize(500,300);
displayWindow.setVisible(true);
}
});
window.setContentPane(content);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(100,100);
window.setSize(800,300);
window.setVisible(true);
}
}