Compare commits
No commits in common. 'c13a40ea955422bdc8eac1c4245c5bb98ad202f8' and '601a1864b38e9d689e5b43b3cb120dbff4c48059' have entirely different histories.
c13a40ea95
...
601a1864b3
@ -1,29 +0,0 @@
|
|||||||
### IntelliJ IDEA ###
|
|
||||||
out/
|
|
||||||
!**/src/main/**/out/
|
|
||||||
!**/src/test/**/out/
|
|
||||||
|
|
||||||
### Eclipse ###
|
|
||||||
.apt_generated
|
|
||||||
.classpath
|
|
||||||
.factorypath
|
|
||||||
.project
|
|
||||||
.settings
|
|
||||||
.springBeans
|
|
||||||
.sts4-cache
|
|
||||||
bin/
|
|
||||||
!**/src/main/**/bin/
|
|
||||||
!**/src/test/**/bin/
|
|
||||||
|
|
||||||
### NetBeans ###
|
|
||||||
/nbproject/private/
|
|
||||||
/nbbuild/
|
|
||||||
/dist/
|
|
||||||
/nbdist/
|
|
||||||
/.nb-gradle/
|
|
||||||
|
|
||||||
### VS Code ###
|
|
||||||
.vscode/
|
|
||||||
|
|
||||||
### Mac OS ###
|
|
||||||
.DS_Store
|
|
@ -1,8 +0,0 @@
|
|||||||
# 默认忽略的文件
|
|
||||||
/shelf/
|
|
||||||
/workspace.xml
|
|
||||||
# 基于编辑器的 HTTP 客户端请求
|
|
||||||
/httpRequests/
|
|
||||||
# Datasource local storage ignored files
|
|
||||||
/dataSources/
|
|
||||||
/dataSources.local.xml
|
|
@ -1,6 +0,0 @@
|
|||||||
<?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>
|
|
@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectModuleManager">
|
|
||||||
<modules>
|
|
||||||
<module fileurl="file://$PROJECT_DIR$/Computer.iml" filepath="$PROJECT_DIR$/Computer.iml" />
|
|
||||||
</modules>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
@ -1,11 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="GitSharedSettings">
|
|
||||||
<option name="FORCE_PUSH_PROHIBITED_PATTERNS">
|
|
||||||
<list />
|
|
||||||
</option>
|
|
||||||
</component>
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
@ -1,11 +0,0 @@
|
|||||||
<?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$/src" isTestSource="false" />
|
|
||||||
</content>
|
|
||||||
<orderEntry type="inheritedJdk" />
|
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
|
||||||
</component>
|
|
||||||
</module>
|
|
@ -1,119 +0,0 @@
|
|||||||
package computer;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import java.text.MessageFormat;
|
|
||||||
import java.util.Arrays;
|
|
||||||
public class com {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
new JS();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
class JS extends JFrame implements ActionListener {
|
|
||||||
private final StringBuilder sBuilder = new StringBuilder();
|
|
||||||
JTextArea text = new JTextArea();
|
|
||||||
double a, b;
|
|
||||||
Double sum;
|
|
||||||
String lastOperator = "=";
|
|
||||||
public JS() {
|
|
||||||
setBounds(100, 100, 400, 400);
|
|
||||||
setTitle("计算器");
|
|
||||||
JMenuBar menubar = new JMenuBar();
|
|
||||||
this.setJMenuBar(menubar);
|
|
||||||
this.setLayout(new BorderLayout());
|
|
||||||
JPanel p1 = new JPanel();
|
|
||||||
JPanel p2 = new JPanel();
|
|
||||||
text.setPreferredSize(new Dimension(370, 60));
|
|
||||||
p2.setLayout(new FlowLayout());
|
|
||||||
p1.add(text);
|
|
||||||
this.add(p1, BorderLayout.NORTH);
|
|
||||||
p2.setLayout(new GridLayout(5, 4));
|
|
||||||
JButton[] button = new JButton[20];
|
|
||||||
button[0] = new JButton("C");
|
|
||||||
button[1] = new JButton("CE");
|
|
||||||
button[2] = new JButton("%");
|
|
||||||
button[3] = new JButton("÷");
|
|
||||||
button[4] = new JButton("7");
|
|
||||||
button[5] = new JButton("8");
|
|
||||||
button[6] = new JButton("9");
|
|
||||||
button[7] = new JButton("*");
|
|
||||||
button[8] = new JButton("4");
|
|
||||||
button[9] = new JButton("5");
|
|
||||||
button[10] = new JButton("6");
|
|
||||||
button[11] = new JButton("-");
|
|
||||||
button[12] = new JButton("1");
|
|
||||||
button[13] = new JButton("2");
|
|
||||||
button[14] = new JButton("3");
|
|
||||||
button[15] = new JButton("+");
|
|
||||||
button[16] = new JButton(" ");
|
|
||||||
button[17] = new JButton("0");
|
|
||||||
button[18] = new JButton(".");
|
|
||||||
button[19] = new JButton("=");
|
|
||||||
Arrays.stream(button).forEach(p2::add);
|
|
||||||
this.add(p2, BorderLayout.CENTER);
|
|
||||||
for (JButton jButton : button) {
|
|
||||||
jButton.addActionListener(this);
|
|
||||||
}
|
|
||||||
this.setVisible(true);
|
|
||||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
}
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
String lab = e.getActionCommand();
|
|
||||||
|
|
||||||
if (Character.isDigit(lab.charAt(0)) || lab.equals(".")) {
|
|
||||||
sBuilder.append(lab);
|
|
||||||
text.setText(sBuilder.toString());
|
|
||||||
} else if (lab.equals("CE")) {
|
|
||||||
sBuilder.deleteCharAt(sBuilder.length() - 1);
|
|
||||||
text.setText(sBuilder.toString());
|
|
||||||
} else if (lab.equals("C")) {
|
|
||||||
sBuilder.setLength(0);
|
|
||||||
text.setText("");
|
|
||||||
} else if (lab.equals("+") || lab.equals("-") || lab.equals("*") || lab.equals("÷") || lab.equals("%")) {
|
|
||||||
if (sBuilder.length() > 0) {
|
|
||||||
a = Double.parseDouble(sBuilder.toString());
|
|
||||||
sBuilder.setLength(0);
|
|
||||||
lastOperator = lab;
|
|
||||||
text.setText(MessageFormat.format("{0} {1} ", a, lastOperator));
|
|
||||||
}
|
|
||||||
} else if (lab.equals("=")) {
|
|
||||||
if (lastOperator != null && !lastOperator.isEmpty() && sBuilder.length() > 0) {
|
|
||||||
b = Double.parseDouble(sBuilder.toString());
|
|
||||||
calculate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private void calculate() {
|
|
||||||
switch (lastOperator) {
|
|
||||||
case "+":
|
|
||||||
sum = a + b;
|
|
||||||
break;
|
|
||||||
case "-":
|
|
||||||
sum = a - b;
|
|
||||||
break;
|
|
||||||
case "*":
|
|
||||||
sum = a * b;
|
|
||||||
break;
|
|
||||||
case "÷":
|
|
||||||
if (b == 0) {
|
|
||||||
text.setText("除数不能为0");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
sum = a / b;
|
|
||||||
break;
|
|
||||||
case "%":
|
|
||||||
if (b == 0) {
|
|
||||||
text.setText("除数不能为0");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
sum = a % b;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
text.setText(sum + "");
|
|
||||||
sBuilder.setLength(0);
|
|
||||||
sBuilder.append(sum);
|
|
||||||
lastOperator = "=";
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue