-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
107 lines (94 loc) · 3.73 KB
/
main.java
File metadata and controls
107 lines (94 loc) · 3.73 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
package gpaCalculator;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class main {
public static void main(String[] args) {
System.out.println("Starting");
JFrame frame = new JFrame("Gpa Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,600);
//frame.setBackground(new Color(217, 215, 215));
gpaPanel gpaInput = new gpaPanel();
frame.add(gpaInput);
frame.setVisible(true);
}
}
class gpaPanel extends JPanel{
JTextField currentField;
gpaAverage currentGpa = new gpaAverage();
JTextField total;
public gpaPanel(){
setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
JLabel totalLabel = new JLabel("Total GPA: ");
topPanel.setLayout(new FlowLayout());
total = new JTextField("0.0");
total.setEditable(false);
total.setPreferredSize(new Dimension(100,40));
total.setFont(new Font("Arial", Font.BOLD, 30));
totalLabel.setFont(new Font("Arial", Font.BOLD, 30));
topPanel.setPreferredSize(new Dimension(0,100));
topPanel.setBackground(new Color(30, 30, 30));
totalLabel.setForeground(Color.WHITE);
total.setForeground(Color.BLACK);
topPanel.add(totalLabel);
topPanel.add(total);
add(topPanel, BorderLayout.SOUTH);
JPanel topRightPanel = new JPanel();
topRightPanel.setLayout(new BorderLayout());
JLabel rLabel = new JLabel("GPA");
JTextField inputGpa = new JTextField();
inputGpa.setPreferredSize(new Dimension(100,40));
currentField = inputGpa;
JPanel wrapper = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton add = new JButton("Add");
add.addActionListener(new ADD());
wrapper.add(inputGpa);
wrapper.add(add);
wrapper.setOpaque(false);
rLabel.setHorizontalAlignment(SwingConstants.CENTER);
inputGpa.setFont(new Font("Arial", Font.BOLD, 20));
rLabel.setFont(new Font("Arial", Font.BOLD, 20));
//rLabel.setPreferredSize(new Dimension(0,200));
topRightPanel.setPreferredSize(new Dimension(0,400));
topRightPanel.add(rLabel, BorderLayout.NORTH);
topRightPanel.add(wrapper, BorderLayout.CENTER);
topRightPanel.setBackground(new Color(30, 30, 30));
rLabel.setForeground(Color.WHITE);
add(topRightPanel, BorderLayout.NORTH);
JPanel totalGpa = new JPanel();
JButton totalButton = new JButton("Calculate Total");
totalButton.addActionListener(new Listen());
totalButton.setFont(new Font("Segoe UI", Font.BOLD, 20));
totalGpa.setBackground(new Color(30, 30, 30));
totalGpa.add(totalButton);
add(totalGpa, BorderLayout.CENTER);
}
private class Listen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
double value = currentGpa.calculate();
total.setText(String.format("%.2f", value));
currentGpa.clear();
}
}
private class ADD implements ActionListener{
@Override
public void actionPerformed(ActionEvent x){
double quantity;
try {
quantity = Double.parseDouble(currentField.getText());
} catch (Exception e) {
quantity = 0;
}
currentGpa.gpa(quantity);
currentField.setText("");
}
}
}