-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicJFrameWithMultipleListeners.java
More file actions
executable file
·108 lines (87 loc) · 3 KB
/
BasicJFrameWithMultipleListeners.java
File metadata and controls
executable file
·108 lines (87 loc) · 3 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
108
import javax.swing.*;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
// Example with implementing multiple listeners -- since this is a lot more
// clean if you have multiple components that requires event listeners
public class BasicJFrameWithMultipleListeners {
JFrame frame;
JPanel myPanel;
JLabel prompt1, prompt2, prompt3, stat;
JTextField grade1, grade2, grade3;
JButton avgButton, minButton, maxButton;
public BasicJFrameWithMultipleListeners () {
frame = new JFrame ("Basic JFrame With Multiple Listeners Example");
frame.setPreferredSize(new Dimension(400, 250));
frame.setLocation(200, 200);
myPanel = new JPanel ();
myPanel.setLayout(new GridLayout (0,2,10,5));
myPanel.setBorder (BorderFactory.createEmptyBorder (10,10,10,10));
prompt1 = new JLabel ("Enter the first grade: ");
grade1 = new JTextField (10);
prompt2 = new JLabel ("Enter the second grade: ");
grade2 = new JTextField (10);
prompt3 = new JLabel ("Enter the third grade: ");
grade3 = new JTextField (10);
avgButton = new JButton ("Average");
avgButton.addActionListener (new AvgListener ());
minButton = new JButton ("Min");
minButton.addActionListener (new MinMaxListener ());
minButton.setActionCommand ("Min");
maxButton = new JButton ("Max");
maxButton.addActionListener (new MinMaxListener ());
maxButton.setActionCommand ("Max");
stat = new JLabel (" ");
stat.setBorder (BorderFactory.createEmptyBorder (10,0,10,10));
myPanel.add (prompt1);
myPanel.add (grade1);
myPanel.add (prompt2);
myPanel.add (grade2);
myPanel.add (prompt3);
myPanel.add (grade3);
myPanel.add (avgButton);
myPanel.add (minButton);
myPanel.add (maxButton);
myPanel.add (stat);
frame.add (myPanel);
frame.pack ();
frame.setVisible (true);
}
class AvgListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
double avgGrade;
String m1 = grade1.getText ();
String m2 = grade2.getText ();
String m3 = grade3.getText ();
avgGrade = Double.parseDouble(m1) + Double.parseDouble (m2) + Double.parseDouble(m3);
avgGrade /= 3;
stat.setText(Double.toString (avgGrade));
}
}
class MinMaxListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
String eventName = event.getActionCommand ();
double minGrade = 999;
double maxGrade = 0;
double [] grades = new double [3];
grades [0] = Double.parseDouble(grade1.getText ());
grades [1] = Double.parseDouble(grade2.getText ());
grades [2] = Double.parseDouble(grade3.getText ());
if (eventName.equals ("Min")) {
for (int i=0; i < 3; i++)
if (minGrade > grades [i])
minGrade = grades [i];
stat.setText(Double.toString(minGrade));
}
else if (eventName.equals ("Max")) {
for (int i=0; i < 3; i++)
if (maxGrade < grades [i])
maxGrade = grades [i];
stat.setText(Double.toString(maxGrade));
}
}
}
public static void main (String [] args){
new BasicJFrameWithMultipleListeners ();
}
}