陈志嘉 2 months ago
parent 67d8df86ac
commit f6aa2550cc

3
.idea/.gitignore vendored

@ -0,0 +1,3 @@
# 默认忽略的文件
/shelf/
/workspace.xml

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8 (2)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Calculator.iml" filepath="$PROJECT_DIR$/.idea/Calculator.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -1,97 +1,20 @@
import javax.swing.*; import javafx.application.Application;
import java.awt.*; import javafx.fxml.FXMLLoader;
import java.awt.event.ActionEvent; import javafx.scene.Scene;
import java.awt.event.ActionListener; import javafx.stage.Stage;
public class Calculator extends JFrame implements ActionListener { public class Calculator extends Application {
private JTextField display;
private double num1 = 0, num2 = 0, result = 0;
private String operation = "";
public Calculator() {
setTitle("简易计算器");
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 显示屏
display = new JTextField();
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);
// 按钮面板
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 10, 10));
String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "C", "0", "=", "+"};
for (String text : buttons) {
JButton button = new JButton(text);
button.addActionListener(this);
panel.add(button);
}
add(panel, BorderLayout.CENTER);
setVisible(true);
}
@Override @Override
public void actionPerformed(ActionEvent e) { public void start(Stage primaryStage) throws Exception {
String command = e.getActionCommand(); FXMLLoader loader = new FXMLLoader(getClass().getResource("Calculator.fxml"));
if (command.equals("C")) { Scene scene = new Scene(loader.load());
clearDisplay(); primaryStage.setTitle("简易计算器");
} else if (command.equals("=")) { primaryStage.setScene(scene);
calculateResult(); primaryStage.show();
} else {
handleInput(command);
}
}
private void handleInput(String input) {
if (input.matches("[0-9]")) {
display.setText(display.getText() + input);
} else {
operation = input;
num1 = Double.parseDouble(display.getText());
display.setText("");
}
}
private void calculateResult() {
num2 = Double.parseDouble(display.getText());
switch (operation) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 != 0) {
result = num1 / num2;
} else {
display.setText("Error");
return;
}
break;
case "%":
result = num1 % num2;
break;
}
display.setText(String.valueOf(result));
}
private void clearDisplay() {
display.setText("");
num1 = 0;
num2 = 0;
result = 0;
operation = "";
} }
public static void main(String[] args) { public static void main(String[] args) {
new Calculator(); launch(args);
} }
} }

@ -0,0 +1,122 @@
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
public class CalculatorController {
@FXML
private TextField inputField;
@FXML
private TextField inputField1; // 用于显示计算记录
private double firstNumber = 0;
private String operator = "";
private boolean isOperationClicked = false;
@FXML
protected void handleNumberAction(ActionEvent event) {
Button button = (Button) event.getSource();
String number = button.getText();
if (isOperationClicked) {
inputField.clear();
isOperationClicked = false;
}
inputField.setText(inputField.getText() + number);
}
@FXML
protected void handleAddAction(ActionEvent event) {
performOperation("+");
}
@FXML
protected void handleSubtractAction(ActionEvent event) {
performOperation("-");
}
@FXML
protected void handleMultiplyAction(ActionEvent event) {
performOperation("*");
}
@FXML
protected void handleDivideAction(ActionEvent event) {
performOperation("/");
}
@FXML
protected void handleModulusAction(ActionEvent event) {
performOperation("%");
}
private void performOperation(String op) {
firstNumber = Double.parseDouble(inputField.getText());
operator = op;
isOperationClicked = true;
}
@FXML
protected void handleEqualAction(ActionEvent event) {
double secondNumber = Double.parseDouble(inputField.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 {
inputField.setText("Error");
return;
}
break;
case "%":
result = firstNumber % secondNumber;
break;
default:
return;
}
inputField.setText(String.valueOf(result));
recordOperation(firstNumber, secondNumber, result);
}
private void recordOperation(double a, double b, double result) {
String operation = a + " " + operator + " " + b + " = " + result;
inputField1.setText(inputField1.getText() + "\n" + operation);
}
@FXML
protected void handleClearAction(ActionEvent event) {
inputField.clear();
firstNumber = 0;
operator = "";
isOperationClicked = false;
}
@FXML
protected void handleClearsAction(ActionEvent event) {
inputField1.clear();
}
@FXML
protected void handlePointAction(ActionEvent event) {
if (!inputField.getText().contains(".")) {
inputField.setText(inputField.getText() + ".");
}
}
@FXML
protected void handleRemoveAction(ActionEvent event) {
inputField1.clear();
}
}

@ -0,0 +1,3 @@
# 默认忽略的文件
/shelf/
/workspace.xml

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8 (2)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Calculator.iml" filepath="$PROJECT_DIR$/.idea/Calculator.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="341.0" prefWidth="375.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="CalculatorController">
<TextField fx:id="inputField" editable="false" layoutX="14.0" layoutY="14.0" prefHeight="80.0" prefWidth="210.0" />
<GridPane hgap="10" layoutX="20.0" layoutY="70.0" vgap="10">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
</GridPane>
<Button graphicTextGap="5.0" layoutX="30.0" layoutY="100.0" onAction="#handleAddAction" prefHeight="30.0" prefWidth="30.0" text="+" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button layoutX="180.0" layoutY="250.0" onAction="#handleEqualAction" prefHeight="80.0" prefWidth="30.0" text="=" />
<Button layoutX="180.0" layoutY="150.0" onAction="#handleClearAction" prefHeight="30.0" prefWidth="30.0" text="C" />
<Button graphicTextGap="5.0" layoutX="80.0" layoutY="100.0" onAction="#handleSubtractAction" prefHeight="30.0" prefWidth="30.0" text="-" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="130.0" layoutY="150.0" onAction="#handleNumberAction" prefHeight="30.0" prefWidth="30.0" text="9" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="80.0" layoutY="150.0" onAction="#handleNumberAction" prefHeight="30.0" prefWidth="30.0" text="8" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="30.0" layoutY="150.0" onAction="#handleNumberAction" prefHeight="30.0" prefWidth="30.0" text="7" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="130.0" layoutY="100.0" onAction="#handleMultiplyAction" prefHeight="30.0" prefWidth="30.0" text="*" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="130.0" layoutY="200.0" onAction="#handleNumberAction" prefHeight="30.0" prefWidth="30.0" text="6" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="80.0" layoutY="200.0" onAction="#handleNumberAction" prefHeight="30.0" prefWidth="30.0" text="5" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="30.0" layoutY="200.0" onAction="#handleNumberAction" prefHeight="30.0" prefWidth="30.0" text="4" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="180.0" layoutY="100.0" onAction="#handleDivideAction" prefHeight="30.0" prefWidth="30.0" text="/" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="130.0" layoutY="250.0" onAction="#handleNumberAction" prefHeight="30.0" prefWidth="30.0" text="3" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="80.0" layoutY="250.0" onAction="#handleNumberAction" prefHeight="30.0" prefWidth="30.0" text="2" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="30.0" layoutY="250.0" onAction="#handleNumberAction" prefHeight="30.0" prefWidth="30.0" text="1" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="130.0" layoutY="300.0" onAction="#handlePointAction" prefHeight="30.0" prefWidth="30.0" text="." wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button layoutX="30.0" layoutY="300.0" onAction="#handleModulusAction" prefHeight="30.0" prefWidth="30.0" text="\%" />
<Button graphicTextGap="5.0" layoutX="80.0" layoutY="300.0" onAction="#handleNumberAction" prefHeight="30.0" prefWidth="30.0" text="0" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<Button graphicTextGap="5.0" layoutX="180.0" layoutY="200.0" onAction="#handleClearsAction" prefHeight="30.0" prefWidth="30.0" text="A" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<TextField fx:id="inputField1" editable="false" layoutX="225.0" layoutY="99.0" prefHeight="230.0" prefWidth="140.0" />
<TextField fx:id="inputField2" editable="false" layoutX="230.0" layoutY="20.0" prefHeight="20.0" prefWidth="120.0" text="计算器计算记录" />
<Button graphicTextGap="5.0" layoutX="250.0" layoutY="60.0" onAction="#handleRemoveAction" text="清空记录" wrapText="true">
<font>
<Font size="15.0" />
</font>
</Button>
<!-- <View layoutX="-134.0" layoutY="-155.0" />-->
</AnchorPane>
Loading…
Cancel
Save