-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradeInputGUI.java
More file actions
55 lines (48 loc) · 1.88 KB
/
GradeInputGUI.java
File metadata and controls
55 lines (48 loc) · 1.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
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class GradeInputGUI {
public static void main(String[] args) {
JFrame frame = new JFrame("Save Data");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("Enter the data");
userLabel.setBounds(10, 10, 100, 25);
panel.add(userLabel);
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(10, 40, 260, 100);
panel.add(scrollPane);
JButton saveButton = new JButton("Save");
saveButton.setBounds(10, 150, 80, 25);
panel.add(saveButton);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] lines = textArea.getText().split("\n");
try (DataOutputStream output = new DataOutputStream(new FileOutputStream("grades.dat"))) {
double total = 0;
int count = 0;
for (String line : lines) {
String[] parts = line.split(" ");
output.writeUTF(parts[0]);
int grade = Integer.parseInt(parts[1]);
output.writeInt(grade);
total += grade;
count++;
}
double average = total / count;
output.writeDouble(average);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}