|
|
|
@ -9,7 +9,6 @@ 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,14 +19,12 @@ 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", "*",
|
|
|
|
|
"1", "2", "3", "-",
|
|
|
|
|
"C", "0", "=", "+"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (String text : buttons) {
|
|
|
|
|
JButton button = new JButton(text);
|
|
|
|
|
button.setFont(new Font("Arial", Font.PLAIN, 24));
|
|
|
|
@ -35,7 +32,6 @@ public class Calculator extends JFrame implements ActionListener {
|
|
|
|
|
button.setBackground(Color.WHITE);
|
|
|
|
|
panel.add(button);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add(display, BorderLayout.NORTH);
|
|
|
|
|
add(panel, BorderLayout.CENTER);
|
|
|
|
|
setTitle("Calculator");
|
|
|
|
@ -63,12 +59,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();
|
|
|
|
|