package com.gaze.notcalculator.mainwindows;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class MainWindows extends Application {
TextField display;
@Override
public void start(Stage stage) throws Exception {
stage.setTitle(" 計算器");
stage.getIcons().add(new Image(getClass().getResource("/image/jsq.png").toExternalForm()));
stage.setResizable(false);
//主容器
BorderPane borderPane = new BorderPane();
//上部區域容器
HBox hBox = new HBox();
VBox vBox = new VBox();
BorderPane borderPane2 = new BorderPane();
hBox.getChildren().add(borderPane2);
ImageView imageView = new ImageView(new Image(getClass().getResource("/image/gn.png").toExternalForm()));
imageView.setFitWidth(20);
imageView.setFitHeight(20);
borderPane2.setPadding(new Insets(100,10,0,10));
//新增螢幕
display= new TextField("0");
display.setFont(new Font("Arial", 36));
display.setStyle("-fx-background-color:#f19123;");
display.setAlignment(Pos.CENTER_RIGHT);
display.setEditable(false);
display.setPrefHeight(100);
VBox.setMargin(imageView, new Insets(-80, 15, 0, 0));
imageView.setOnMousePressed(e -> imageView.setStyle("-fx-background-radius: 10; -fx-background-color:#f4f7f9; -fx-scale-x: 0.95; -fx-scale-y: 0.95;"));
imageView.setOnMouseReleased(e -> imageView.setStyle("-fx-background-radius: 10; -fx-background-color: #ffffff; -fx-scale-x: 1; -fx-scale-y: 1;"));
//設定選單
ContextMenu contextMenu = new ContextMenu();
//新增選單項
MenuItem historyItem = new MenuItem("歷史記錄");
MenuItem pwdItem = new MenuItem("設定密碼");
contextMenu.getItems().addAll(historyItem, pwdItem);
//設定點選按鈕
imageView.setOnMouseClicked(e -> contextMenu.show(imageView, e.getScreenX(), e.getScreenY()));
borderPane2.setBottom(display);
borderPane2.setRight(vBox);
vBox.getChildren().add(imageView);
// 中心區域容器
GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(0, 0,0,0));
// gridPane.prefWidth(390);
// gridPane.prefHeight(360);
// 建立按鈕
String[] buttonsText = {
"AC", "%","⌫","÷",
"7", "8", "9", "×",
"4", "5", "6", "-",
"1", "2", "3", "+",
"00", "0", ".","="
}; // 配置列和行的大小
// 新增按鈕到 GridPane
int index = 0;
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 4; col++) {
if (index < buttonsText.length) {
Button button = new Button(buttonsText[index]);
button.setFont(new Font("Arial", 18));
button.setPrefSize(90, 70);
button.setStyle("-fx-background-radius: 10;-fx-background-color: #ffffff");
//滑鼠進入、退出
button.setOnMouseEntered(e -> button.setStyle("-fx-background-color: #f4f7f9"));
button.setOnMouseExited(e -> button.setStyle("-fx-background-color: #ffffff"));
button.setOnMousePressed(e -> button.setStyle("-fx-background-radius: 10; -fx-background-color:#f4f7f9; -fx-scale-x: 0.95; -fx-scale-y: 0.95;"));
button.setOnMouseReleased(e -> button.setStyle("-fx-background-radius: 10; -fx-background-color: #ffffff; -fx-scale-x: 1; -fx-scale-y: 1;"));
// button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
button.setOnAction(e -> handleButtonAction(button.getText()));
index++;
gridPane.add(button, col, row);
}
}
}
gridPane.setHgap(5);
gridPane.setVgap(5);
gridPane.setAlignment(Pos.CENTER);
for (int i = 0; i < 4; i++) {
ColumnConstraints column = new ColumnConstraints(90); // 每列寬度為80
gridPane.getColumnConstraints().add(column);
}
for (int i = 0; i < 5; i++) {
RowConstraints row = new RowConstraints(40); // 每行高度為80
gridPane.getRowConstraints().add(row);
}
gridPane.setStyle("-fx-background-color: #eff4f9;");
borderPane.setTop(hBox);
borderPane.setCenter(gridPane);
Scene scene = new Scene(borderPane,400,500);
stage.setScene(scene);
stage.show();
}
private void handleButtonAction(String buttonText) {
String currentText = display.getText();
System.out.println(buttonText);
switch (buttonText) {
case "AC":
display.setText("0");
break;
case "⌫":
if (currentText.length() > 1) {
display.setText(currentText.substring(0, currentText.length() - 1));
} else {
display.setText("0");
}
break;
case "=":
try {
if (currentText.contains("×")){
// 查詢運算子的位置
int operatorIndex = currentText.indexOf("×");
// 如果找到運算子
if (operatorIndex != -1) {
// 擷取運算子前面的部分
String beforeOperator = currentText.substring(0, operatorIndex);
// 擷取運算子後面的部分
String afterOperator = currentText.substring(operatorIndex + 1);
Double result = Double.parseDouble(beforeOperator) * Double.parseDouble(afterOperator);
System.out.println(result);
display.setText(String.valueOf(result));
} else {
display.setText("輸入不合符!");
}
} else if (currentText.contains("÷")){
// 查詢運算子的位置
int operatorIndex = currentText.indexOf("÷");
// 如果找到運算子
if (operatorIndex != -1) {
// 擷取運算子前面的部分
String beforeOperator = currentText.substring(0, operatorIndex);
// 擷取運算子後面的部分
String afterOperator = currentText.substring(operatorIndex + 1);
Double result = Double.parseDouble(beforeOperator) / Double.parseDouble(afterOperator);
System.out.println(result);
display.setText(String.valueOf(result));
} else {
display.setText("輸入不合符!");
}
}else if (currentText.contains("+")){
// 查詢運算子的位置
int operatorIndex = currentText.indexOf("+");
// 如果找到運算子
if (operatorIndex != -1) {
// 擷取運算子前面的部分
String beforeOperator = currentText.substring(0, operatorIndex);
// 擷取運算子後面的部分
String afterOperator = currentText.substring(operatorIndex + 1);
Double result = Double.parseDouble(beforeOperator) + Double.parseDouble(afterOperator);
System.out.println(result);
display.setText(String.valueOf(result));
} else {
display.setText("輸入不合符!");
}
}else if (currentText.contains("-")){
// 查詢運算子的位置
int operatorIndex = currentText.indexOf("-");
// 如果找到運算子
if (operatorIndex != -1) {
// 擷取運算子前面的部分
String beforeOperator = currentText.substring(0, operatorIndex);
// 擷取運算子後面的部分
String afterOperator = currentText.substring(operatorIndex + 1);
Double result = Double.parseDouble(beforeOperator) - Double.parseDouble(afterOperator);
System.out.println(result);
display.setText(String.valueOf(result));
} else {
display.setText("輸入不合符!");
}
}else if (currentText.contains("%")){
// 查詢運算子的位置
int operatorIndex = currentText.indexOf("%");
// 如果找到運算子
if (operatorIndex != -1) {
// 擷取運算子前面的部分
String beforeOperator = currentText.substring(0, operatorIndex);
// 擷取運算子後面的部分
System.out.println(beforeOperator);
String afterOperator = currentText.substring(operatorIndex + 1);
System.out.println(afterOperator);
if (afterOperator.equals("")){
Double result = Double.parseDouble(beforeOperator) / 100;
display.setText(String.valueOf(result));
}else {
Double result = Double.parseDouble(beforeOperator) / 100 * Double.parseDouble(afterOperator);
display.setText(String.valueOf(result));
}
} else {
display.setText("輸入不合符!");
}
}
} catch (Exception e){
display.setText("輸入不合符!");
}
break;
default:
if (currentText.equals("0")) {
display.setText(buttonText);
} else {
display.setText(currentText + buttonText);
}
break;
}
}
public static void main(String[] args) {
launch();
}
}