-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java~
More file actions
153 lines (113 loc) · 4.15 KB
/
Copy pathCalculator.java~
File metadata and controls
153 lines (113 loc) · 4.15 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.util.Stack;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Calculator extends Application{
//the calculator dimensions
public static int CALC_WIDTH = 400;
public static int CALC_HEIGHT = 300;
//adding opereator and operand data members
private Stack<Double> operandStack = new Stack<>();
private Stack<Operator> operatorStack = new Stack<>();
//holds the current number being built from the press of the button
private String currentNumber = "";
//the calculator screen
private TextField screen;
//the calculator buttons
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
/**
* This method will append digits to the number that is being built from the number presses
* **/
private void appendDigit(String digit){
//add digit to the number
currentNumber += digit;
screen.setText(currentNumber);
}
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage){
//create calculator screen
screen = new TextField();
//creating buttons
button1 = new Button("1");
button2 = new Button("2");
button3 = new Button("3");
button4 = new Button("4");
button5 = new Button("+");
button6 = new Button("=");
//attach handler to process clicks
ButtonHandler handler = new ButtonHandler();
button1.setOnAction(handler);
button2.setOnAction(handler);
button3.setOnAction(handler);
button4.setOnAction(handler);
button5.setOnAction(handler);
button6.setOnAction(handler);
//set up a grid panel for the keyboard
GridPane keypad = new GridPane();
keypad.setMinSize(CALC_WIDTH, CALC_HEIGHT);
//keypad.setPadding(new Insets(10, 10, 10, 10));
//keypad.setVGap(5);
//keypad.setHGap(5);
keypad.setAlignment(Pos.CENTER);
//attach the buttons to the keypad grid
//have different columns and rows for each button
keypad.add(button1, 0, 0);
keypad.add(button2, 1, 0);
keypad.add(button3, 0, 1);
keypad.add(button4, 1, 1);
keypad.add(button5, 2, 0);
keypad.add(button6, 2, 1);
//put screen and keypad together
BorderPane gui = new BorderPane();
gui.setTop(screen);
gui.setCenter(keypad);
//set up the scene
Scene scene = new Scene(gui);
primaryStage.setTitle("Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
//handler for processing the button clicks
private class ButtonHandler implements EventHandler<ActionEvent>{
/**
* we are going to add number buttons here
* we are going to call appenddigit method to build the number from the buttons being pressed
*/
@Override
public void handle(ActionEvent e){
if(e.getSource() == button1){
appendDigit("1");
}
else if(e.getSource() == button2){
appendDigit("2");
}
else if(e.getSource() == button3){
appendDigit("3");
}
else if(e.getSource() == button4){
appendDigit("4");
}
else if(e.getSource() == button5){
appendDigit("+");
}
else if(e.getSource() == button6){
appendDigit("=");
}
}
}
}