I know there are probably many problems with this code as well as the way I did it, but the main thing I can't figure out to do is set more than one digit in the Text area when pushing buttons (if i push 1, then 2, then 3, 3 appears instead of 123).
Obviously this is a calculator program...........
I tried making it so that each time someone pushes a button, it adds that number to a string which it then displays.
Yes, I know the operation buttons do nothing and the calculator layout itself is pretty crappy.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CalculatorEdit extends JFrame implements ActionListener
{
public static final int WIDTH = 600;
public static final int HEIGHT = 200;
private JTextField inputOutputField;
public static void main(String[] args)
{
Calculator guiWithPanel = new Calculator();
guiWithPanel.setVisible(true);
}
public CalculatorEdit()
{
inputOutputField = new JTextField("0", 30);
setSize(WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer());
setTitle("Gabens Miniature Calculator");
Container contentPane = getContentPane();
JPanel operationPanel = new JPanel();
JPanel buttonPanel = new JPanel();
contentPane.setLayout(new BorderLayout());
JButton zeroButton = new JButton("0");
zeroButton.addActionListener(this);
buttonPanel.add(zeroButton);
JButton oneButton = new JButton("1");
oneButton.addActionListener(this);
buttonPanel.add(oneButton);
JButton twoButton = new JButton("2");
twoButton.addActionListener(this);
buttonPanel.add(twoButton);
JButton threeButton = new JButton("3");
threeButton.addActionListener(this);
buttonPanel.add(threeButton);
JButton fourButton = new JButton("4");
fourButton.addActionListener(this);
buttonPanel.add(fourButton);
JButton fiveButton = new JButton("5");
fiveButton.addActionListener(this);
buttonPanel.add(fiveButton);
JButton sixButton = new JButton("6");
sixButton.addActionListener(this);
buttonPanel.add(sixButton);
JButton sevenButton = new JButton("7");
sevenButton.addActionListener(this);
buttonPanel.add(sevenButton);
JButton eightButton = new JButton("8");
eightButton.addActionListener(this);
buttonPanel.add(eightButton);
JButton nineButton = new JButton("9");
nineButton.addActionListener(this);
buttonPanel.add(nineButton);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
JButton addButton = new JButton("+");
addButton.addActionListener(this);
operationPanel.add(addButton);
JButton subtractButton = new JButton("-");
subtractButton.addActionListener(this);
operationPanel.add(subtractButton);
JButton multiplyButton = new JButton("*");
multiplyButton.addActionListener(this);
operationPanel.add(multiplyButton);
JButton divideButton = new JButton("/");
divideButton.addActionListener(this);
operationPanel.add(divideButton);
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(this);
operationPanel.add(resetButton);
JButton equalButton = new JButton("=");
equalButton.addActionListener(this);
operationPanel.add(equalButton);
contentPane.add(operationPanel, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{
Container contentPane = getContentPane();
String a = "";
String b = "";
int answer = 0;
double operation = 0;
if (e.getActionCommand().equals("0"))
{
if (operation == 0)
{
a = a + "0";
inputOutputField.setText(a);
}
else
{
b = b + "0";
inputOutputField.setText(b);
}
}
else if (e.getActionCommand().equals("1"))
{
if (operation == 0)
{
a = a + "1";
inputOutputField.setText(a);
}
else
{
b = b + "1";
inputOutputField.setText(b);
}
}
else if (e.getActionCommand().equals("2"))
{
if (operation == 0)
{
a = a + "2";
inputOutputField.setText(a);
}
else
{
b = b + "2";
inputOutputField.setText(b);
}
}
else if (e.getActionCommand().equals("3"))
{
if (operation == 0)
{
a = a + "3";
inputOutputField.setText(a);
}
else
{
b = b + "3";
inputOutputField.setText(b);
}
}
else if (e.getActionCommand().equals("4"))
{
if (operation == 0)
{
a = a + "4";
inputOutputField.setText(a);
}
else
{
b = b + "4";
inputOutputField.setText(b);
}
}
else if (e.getActionCommand().equals("5"))
{
if (operation == 0)
{
a = a + "5";
inputOutputField.setText(a);
}
else
{
b = b + "5";
inputOutputField.setText(b);
}
}
else if (e.getActionCommand().equals("6"))
{
if (operation == 0)
{
a = a + "6";
inputOutputField.setText(a);
}
else
{
b = b + "6";
inputOutputField.setText(b);
}
}
else if (e.getActionCommand().equals("7"))
{
if (operation == 0)
{
a = a + "7";
inputOutputField.setText(a);
}
else
{
b = b + "7";
inputOutputField.setText(b);
}
}
else if (e.getActionCommand().equals("8"))
{
if (operation == 0)
{
a = a + "8";
inputOutputField.setText(a);
}
else
{
b = b + "8";
inputOutputField.setText(b);
}
}
else if (e.getActionCommand().equals("9"))
{
if (operation == 0)
{
a = a + "9";
inputOutputField.setText(a);
}
else
{
b = b + "9";
inputOutputField.setText(b);
}
}
else if (e.getActionCommand().equals("+"))
{
operation = 1;
}
else if (e.getActionCommand().equals("-"))
{
operation = 2;
}
else if (e.getActionCommand().equals("*"))
{
operation = 3;
}
else if (e.getActionCommand().equals("/"))
{
operation = 4;
}
else if (e.getActionCommand().equals("="))
{
if (operation == 1);
{
answer = (Integer.parseInt(a) + Integer.parseInt(b));
inputOutputField.setText("" + answer);
}
if (operation == 2);
{
answer = (Integer.parseInt(a) - Integer.parseInt(b));
inputOutputField.setText("" + answer);
}
if (operation == 3);
{
answer = (Integer.parseInt(a) * Integer.parseInt(b));
inputOutputField.setText("" + answer);
}
if (operation == 4);
{
answer = (Integer.parseInt(a) / Integer.parseInt(b));
inputOutputField.setText("" + answer);
}
}
else if (e.getActionCommand().equals("Reset"))
{
operation = 0;
a = "";
b = "";
inputOutputField.setText("");
}
else
System.out.println("Error in button interface.");
}
}
__________________