parent
9f4fafd8d5
commit
f753209574
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.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" 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/computer.iml" filepath="$PROJECT_DIR$/.idea/computer.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,87 @@
|
|||||||
|
import javax.script.ScriptEngine;
|
||||||
|
import javax.script.ScriptEngineManager;
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
|
||||||
|
public class Calculator extends JFrame {
|
||||||
|
private JTextField inputField; // 输入显示区域
|
||||||
|
|
||||||
|
public Calculator() {
|
||||||
|
createUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createUI() {
|
||||||
|
// 设置窗口布局
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
// 创建输入显示区域
|
||||||
|
inputField = new JTextField("0");
|
||||||
|
inputField.setEditable(false);
|
||||||
|
inputField.setHorizontalAlignment(JTextField.RIGHT);
|
||||||
|
add(inputField, BorderLayout.NORTH);
|
||||||
|
|
||||||
|
// 创建面板用于放置按钮
|
||||||
|
JPanel panel = new JPanel();
|
||||||
|
panel.setLayout(new GridLayout(5, 4, 5, 5)); // 设置间距
|
||||||
|
|
||||||
|
// 创建数字和运算符按钮
|
||||||
|
String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+", "C", "Backspace"};
|
||||||
|
for (String b : buttons) {
|
||||||
|
JButton button = new JButton(b);
|
||||||
|
panel.add(button);
|
||||||
|
button.addActionListener(e -> press(e.getActionCommand()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加面板到窗口
|
||||||
|
add(panel, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
// 设置窗口属性
|
||||||
|
setSize(300, 400); // 设置窗口大小
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setTitle("Simple Calculator");
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void press(String command) {
|
||||||
|
if ("C".equals(command)) {
|
||||||
|
clear();
|
||||||
|
} else if ("Backspace".equals(command)) {
|
||||||
|
backspace();
|
||||||
|
} else if ("=".equals(command)) {
|
||||||
|
calculate();
|
||||||
|
} else {
|
||||||
|
if (inputField.getText().equals("0") && !command.equals(".")) {
|
||||||
|
inputField.setText(command);
|
||||||
|
} else {
|
||||||
|
inputField.setText(inputField.getText() + command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clear() {
|
||||||
|
inputField.setText("0");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void backspace() {
|
||||||
|
String currentText = inputField.getText();
|
||||||
|
if (!currentText.isEmpty()) {
|
||||||
|
inputField.setText(currentText.substring(0, currentText.length() - 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calculate() {
|
||||||
|
String expression = inputField.getText();
|
||||||
|
try {
|
||||||
|
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
|
||||||
|
Object result = engine.eval(expression);
|
||||||
|
inputField.setText(result.toString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(Calculator::new);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.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" 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/computer.iml" filepath="$PROJECT_DIR$/.idea/computer.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>
|
Binary file not shown.
@ -0,0 +1,2 @@
|
|||||||
|
# computer
|
||||||
|
|
Loading…
Reference in new issue