-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculatorView.java
70 lines (59 loc) · 1.98 KB
/
CalculatorView.java
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
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CalculatorView extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField firstNumber = new JTextField(10);
private JTextField secondNumber = new JTextField(10);
private JButton addButton = new JButton("Add");
private JButton minusButton = new JButton("Minus");
private JButton multipleButton = new JButton("Mutiple");
private JButton divideButton = new JButton("Divide");
private JTextField calcSolution = new JTextField(10);
private JLabel equalsLabel = new JLabel("=");
CalculatorView(){
JPanel calcPanel = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,200);
calcPanel.add(firstNumber);
calcPanel.add(addButton);
calcPanel.add(minusButton);
calcPanel.add(multipleButton);
calcPanel.add(divideButton);
calcPanel.add(secondNumber);
calcPanel.add(equalsLabel);
calcPanel.add(calcSolution);
this.add(calcPanel);
}
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(double solution){
String value = String.format("%.5f", solution);
calcSolution.setText(value);
}
void addCalculationListener(ActionListener listenForCalcButton){
addButton.addActionListener(listenForCalcButton);
}
void minusCalculationListener(ActionListener listenForCalcButton){
minusButton.addActionListener(listenForCalcButton);
}
void divideCalculationListener(ActionListener listenForCalcButton){
divideButton.addActionListener(listenForCalcButton);
}
void multiCalculationListener(ActionListener listenForCalcButton){
multipleButton.addActionListener(listenForCalcButton);
}
void displayError(String errorMessage){
JOptionPane.showMessageDialog(this, errorMessage);
}
}