Compare commits
No commits in common. 'main' and 'master' have entirely different histories.
@ -0,0 +1,3 @@
|
|||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
@ -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$/lesson2.iml" filepath="$PROJECT_DIR$/lesson2.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,81 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
|
import javax.script.ScriptEngineManager;
|
||||||
|
import javax.script.ScriptException;
|
||||||
|
|
||||||
|
public class Calculator extends JFrame implements ActionListener {
|
||||||
|
private JTextField display;
|
||||||
|
private StringBuilder input;
|
||||||
|
public Calculator() {
|
||||||
|
input = new StringBuilder();
|
||||||
|
display = new JTextField();
|
||||||
|
display.setEditable(false);
|
||||||
|
display.setFont(new Font("Arial", Font.BOLD, 32));
|
||||||
|
display.setHorizontalAlignment(JTextField.RIGHT);
|
||||||
|
display.setBackground(Color.LIGHT_GRAY);
|
||||||
|
JPanel panel = new JPanel();
|
||||||
|
panel.setLayout(new GridLayout(5, 4, 10, 10)); // Increase number of rows
|
||||||
|
panel.setBackground(Color.GRAY);
|
||||||
|
String[] buttons = {
|
||||||
|
"7", "8", "9", "/",
|
||||||
|
"4", "5", "6", "*",
|
||||||
|
"1", "2", "3", "-",
|
||||||
|
"C", "0", ".", "+", // Add decimal point
|
||||||
|
"<-", "=", "" // Add delete button
|
||||||
|
};
|
||||||
|
for (String text : buttons) {
|
||||||
|
JButton button = new JButton(text);
|
||||||
|
button.setFont(new Font("Arial", Font.PLAIN, 24));
|
||||||
|
button.addActionListener(this);
|
||||||
|
button.setBackground(Color.WHITE);
|
||||||
|
panel.add(button);
|
||||||
|
}
|
||||||
|
add(display, BorderLayout.NORTH);
|
||||||
|
add(panel, BorderLayout.CENTER);
|
||||||
|
setTitle("Calculator");
|
||||||
|
setSize(400, 500);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
String command = e.getActionCommand();
|
||||||
|
|
||||||
|
if (command.charAt(0) == 'C') {
|
||||||
|
input.setLength(0);
|
||||||
|
display.setText("");
|
||||||
|
} else if (command.equals("<-")) { // Delete the last character
|
||||||
|
if (input.length() > 0) {
|
||||||
|
input.setLength(input.length() - 1);
|
||||||
|
display.setText(input.toString());
|
||||||
|
}
|
||||||
|
} else if (command.charAt(0) == '=') {
|
||||||
|
try {
|
||||||
|
double result = evaluate(input.toString());
|
||||||
|
display.setText(String.valueOf(result));
|
||||||
|
input.setLength(0);
|
||||||
|
} catch (ScriptException ex) {
|
||||||
|
display.setText("Error");
|
||||||
|
input.setLength(0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
input.append(command);
|
||||||
|
display.setText(input.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private double evaluate(String expression) throws ScriptException {
|
||||||
|
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
|
||||||
|
return ((Number) engine.eval(expression)).doubleValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
Calculator calculator = new Calculator();
|
||||||
|
calculator.setVisible(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -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,3 @@
|
|||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
@ -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$/lesson2.iml" filepath="$PROJECT_DIR$/lesson2.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,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>
|
Loading…
Reference in new issue