So i made this code with my friend, but we are having some problems we want to have the "C" button clear the text everytime which it does, unless you have already typed in a problem. Anyway let me know what you think
NOT JAVASCRIPT
/*
* Antonio and Colin's Amazing Calculator
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class CalculatorFrame extends JFrame
{
private static final long serialVersionUID = 1L;
public CalculatorFrame()
{
setTitle("Calculator");
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}
class CalculatorPanel extends JPanel
{
private static final long serialVersionUID = 1L;
public CalculatorPanel()
{
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;
// add the display
display = new JButton("0");
display.setEnabled(false);
display.setBackground(Color.black);
display.setForeground(Color.white);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
// add the buttons in a 4 x 4 grid
panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("/", command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("-", command);
addButton(".", command);
addButton("0", insert);
addButton("C", command);
addButton("+", command);
addButton("", command);
addButton("^2",command);
addButton("", command);
addButton("=", command);
add(panel, BorderLayout.CENTER);
}
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (start)
{
if (command.equals("-"))
{
display.setText(command);
start = true;
}
else lastCommand = command;
}
else
{
lastCommand = command;
calculate(Double.parseDouble(display.getText()));
start = true;
}
}
}
public void calculate(double x)
{
if (lastCommand.equals("C")) result = x * 0.0;
else
{
if (lastCommand.equals("+")) result += x;
else if (lastCommand.equals("-")) result -= x;
else if (lastCommand.equals("*")) result *= x;
else if (lastCommand.equals("/")) result /= x;
else if (lastCommand.equals("=")) result = x;
else if (lastCommand.equals("^2")) result = x * x;
}
display.setText("" + result);
}
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}