|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|