|
|
|
@ -5,11 +5,9 @@ import java.awt.event.ActionListener;
|
|
|
|
|
import javax.script.ScriptEngineManager;
|
|
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
|
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();
|
|
|
|
@ -20,7 +18,6 @@ public class Calculator extends JFrame implements ActionListener {
|
|
|
|
|
JPanel panel = new JPanel();
|
|
|
|
|
panel.setLayout(new GridLayout(4, 4, 10, 10));
|
|
|
|
|
panel.setBackground(Color.GRAY);
|
|
|
|
|
|
|
|
|
|
String[] buttons = {
|
|
|
|
|
"7", "8", "9", "/",
|
|
|
|
|
"4", "5", "6", "*",
|
|
|
|
@ -36,7 +33,6 @@ public class Calculator extends JFrame implements ActionListener {
|
|
|
|
|
}
|
|
|
|
|
add(display, BorderLayout.NORTH);
|
|
|
|
|
add(panel, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
|
|
setTitle("Calculator");
|
|
|
|
|
setSize(400, 500);
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
@ -62,12 +58,10 @@ public class Calculator extends JFrame implements ActionListener {
|
|
|
|
|
display.setText(input.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double evaluate(String expression) throws ScriptException {
|
|
|
|
|
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
|
|
|
|
|
return ((Number) engine.eval(expression)).doubleValue();
|
|
|
|
|
}
|
|
|
|
|
//11122222
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
|
Calculator calculator = new Calculator();
|
|
|
|
|