|
|
|
@ -0,0 +1,153 @@
|
|
|
|
|
import javafx.application.Application;
|
|
|
|
|
import javafx.geometry.Insets;
|
|
|
|
|
import javafx.geometry.Pos;
|
|
|
|
|
import javafx.scene.Scene;
|
|
|
|
|
import javafx.scene.control.Button;
|
|
|
|
|
import javafx.scene.control.Label;
|
|
|
|
|
import javafx.scene.layout.GridPane;
|
|
|
|
|
import javafx.scene.layout.VBox;
|
|
|
|
|
import javafx.stage.Stage;
|
|
|
|
|
public class Calculate extends Application {
|
|
|
|
|
private Label display;
|
|
|
|
|
private double firstNumber = 0;
|
|
|
|
|
private String operator = "";
|
|
|
|
|
private boolean startNewNumber = true;
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
launch(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void start(Stage primaryStage) {
|
|
|
|
|
// Create display label
|
|
|
|
|
display = new Label("0.0");
|
|
|
|
|
display.setStyle("-fx-font-size: 30px;");
|
|
|
|
|
display.setMinWidth(200); // Adjust width for display
|
|
|
|
|
display.setAlignment(Pos.CENTER_RIGHT); // Align text to the right
|
|
|
|
|
|
|
|
|
|
// Set up the buttons
|
|
|
|
|
GridPane gridPane = createButtonGrid();
|
|
|
|
|
|
|
|
|
|
// Create the layout
|
|
|
|
|
VBox layout = new VBox(10);
|
|
|
|
|
layout.setPadding(new Insets(10));
|
|
|
|
|
layout.setAlignment(Pos.CENTER);
|
|
|
|
|
layout.getChildren().addAll(display, gridPane);
|
|
|
|
|
|
|
|
|
|
// Set up the scene and stage
|
|
|
|
|
Scene scene = new Scene(layout, 300, 400);
|
|
|
|
|
primaryStage.setTitle("Calculator");
|
|
|
|
|
primaryStage.setScene(scene);
|
|
|
|
|
primaryStage.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private GridPane createButtonGrid() {
|
|
|
|
|
GridPane grid = new GridPane();
|
|
|
|
|
grid.setHgap(10);
|
|
|
|
|
grid.setVgap(10);
|
|
|
|
|
grid.setAlignment(Pos.CENTER);
|
|
|
|
|
|
|
|
|
|
// Create buttons with a similar layout to your screenshot
|
|
|
|
|
String[][] buttonText = {
|
|
|
|
|
{"AC", "±", "%", "÷"},
|
|
|
|
|
{"7", "8", "9", "×"},
|
|
|
|
|
{"4", "5", "6", "-"},
|
|
|
|
|
{"1", "2", "3", "+"},
|
|
|
|
|
{"0", ".", "=", ""}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Set button size
|
|
|
|
|
int buttonSize = 50;
|
|
|
|
|
|
|
|
|
|
// Add buttons to the grid
|
|
|
|
|
int row = 0;
|
|
|
|
|
for (int i = 0; i < buttonText.length; i++) {
|
|
|
|
|
for (int j = 0; j < buttonText[i].length; j++) {
|
|
|
|
|
if (!buttonText[i][j].isEmpty()) {
|
|
|
|
|
Button button = createButton(buttonText[i][j], buttonSize);
|
|
|
|
|
if (buttonText[i][j].equals("0")) {
|
|
|
|
|
grid.add(button, j, row, 2, 1); // Make '0' span two columns
|
|
|
|
|
} else {
|
|
|
|
|
grid.add(button, j, row);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
row++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return grid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Button createButton(String text, int size) {
|
|
|
|
|
Button button = new Button(text);
|
|
|
|
|
button.setMinSize(size, size);
|
|
|
|
|
button.setStyle("-fx-font-size: 18px; -fx-background-color: white; -fx-border-color: lightgray;");
|
|
|
|
|
button.setOnAction(e -> handleButtonPress(text));
|
|
|
|
|
return button;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleButtonPress(String text) {
|
|
|
|
|
if (text.matches("\\d") || text.equals(".")) {
|
|
|
|
|
handleNumberInput(text);
|
|
|
|
|
} else if (text.equals("AC")) {
|
|
|
|
|
handleClear();
|
|
|
|
|
} else if (text.equals("=")) {
|
|
|
|
|
handleEquals();
|
|
|
|
|
} else {
|
|
|
|
|
handleOperator(text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleNumberInput(String text) {
|
|
|
|
|
if (startNewNumber) {
|
|
|
|
|
display.setText(text);
|
|
|
|
|
startNewNumber = false;
|
|
|
|
|
} else {
|
|
|
|
|
display.setText(display.getText() + text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleClear() {
|
|
|
|
|
display.setText("0");
|
|
|
|
|
firstNumber = 0;
|
|
|
|
|
operator = "";
|
|
|
|
|
startNewNumber = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleOperator(String text) {
|
|
|
|
|
firstNumber = Double.parseDouble(display.getText());
|
|
|
|
|
operator = text;
|
|
|
|
|
startNewNumber = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleEquals() {
|
|
|
|
|
double secondNumber = Double.parseDouble(display.getText());
|
|
|
|
|
double result = 0;
|
|
|
|
|
|
|
|
|
|
switch (operator) {
|
|
|
|
|
case "+":
|
|
|
|
|
result = firstNumber + secondNumber;
|
|
|
|
|
break;
|
|
|
|
|
case "-":
|
|
|
|
|
result = firstNumber - secondNumber;
|
|
|
|
|
break;
|
|
|
|
|
case "×":
|
|
|
|
|
result = firstNumber * secondNumber;
|
|
|
|
|
break;
|
|
|
|
|
case "÷":
|
|
|
|
|
if (secondNumber != 0) {
|
|
|
|
|
result = firstNumber / secondNumber;
|
|
|
|
|
} else {
|
|
|
|
|
display.setText("Error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "%":
|
|
|
|
|
result = firstNumber % secondNumber;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
display.setText(String.valueOf(result));
|
|
|
|
|
startNewNumber = true;
|
|
|
|
|
}
|
|
|
|
|
}
|