Quote (carteblanche @ Apr 22 2015 10:43pm)
for starters, get rid of the semicolon
if (start);
i'm not clear what this is supposed to do either, considering that you don't have an underscore anywhere else in the app:
if (command.equals("_"))
i also suggest adding log statements to every method so you know what is getting called. if you click a button and it's not creating a log statement, you know your listener isn't getting triggered. you have a lot of if statements, but if you're not sure why it's not working, it might not be executing. it should help you figure out the problem.
design-wise, i'd split up the view from the calculations so that you can test it from the command line without worrying about the applet/swing/etc.
eg:
Calculator c = new Calculator();
c.push("1");
c.push("+");
c.push("2");
c.push("="); // returns 3
test it via your command line, then your swing applet uses the exact same logic, just with buttons.
Code
package Calculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
// import necessary info for applet
public class JavaCalculator extends Applet {
private boolean start;
private double result;
private JButton display;
private JPanel panel;
private String lastCommand;
//imported JPanel for setup
public JavaCalculator(){
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;
// add the display
display = new JButton("0");
display.setEnabled(false);
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
//INtroduce grid layout
panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
//Adding buttons to calc
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("0", insert);
addButton(".", insert);
addButton("C", command);
addButton("/", command);
addButton("=", command);
add(panel, BorderLayout.CENTER);
}
//Adds Button
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
//Inserts Action
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);
}
}
//Executes command
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(start)
{
if (command.equals("_"))
{
display.setText(command);
start = false;
}
else lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}
//Performs Final Calculations
public void calculate(double x)
{
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;
display.setText("" + result);
}
}
Okay, so you were saying I should add logs. That sounds like a good idea.
Also, what am I doing wrong regarding the "=" button. I'd like it to fill up the entire bottom part of the screen if possible.
This post was edited by axid3nt on Apr 22 2015 08:58pm