forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorView.java
More file actions
81 lines (48 loc) · 1.75 KB
/
CalculatorView.java
File metadata and controls
81 lines (48 loc) · 1.75 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
// This is the View
// Its only job is to display what the user sees
// It performs no calculations, but instead passes
// information entered by the user to whomever needs
// it.
import java.awt.event.ActionListener;
import javax.swing.*;
public class CalculatorView extends JFrame{
private JTextField firstNumber = new JTextField(10);
private JLabel additionLabel = new JLabel("+");
private JTextField secondNumber = new JTextField(10);
private JButton calculateButton = new JButton("Calculate");
private JTextField calcSolution = new JTextField(10);
CalculatorView(){
// Sets up the view and adds the components
JPanel calcPanel = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 200);
calcPanel.add(firstNumber);
calcPanel.add(additionLabel);
calcPanel.add(secondNumber);
calcPanel.add(calculateButton);
calcPanel.add(calcSolution);
this.add(calcPanel);
// End of setting up the components --------
}
public int getFirstNumber(){
return Integer.parseInt(firstNumber.getText());
}
public int getSecondNumber(){
return Integer.parseInt(secondNumber.getText());
}
public int getCalcSolution(){
return Integer.parseInt(calcSolution.getText());
}
public void setCalcSolution(int solution){
calcSolution.setText(Integer.toString(solution));
}
// If the calculateButton is clicked execute a method
// in the Controller named actionPerformed
void addCalculateListener(ActionListener listenForCalcButton){
calculateButton.addActionListener(listenForCalcButton);
}
// Open a popup that contains the error message passed
void displayErrorMessage(String errorMessage){
JOptionPane.showMessageDialog(this, errorMessage);
}
}