Java :(

Discussion in 'OT Technology' started by jdubb5005, May 5, 2006.

  1. jdubb5005

    jdubb5005 I live in a giant bucket.

    Joined:
    Feb 15, 2006
    Messages:
    120
    Likes Received:
    0
    Location:
    Green Bay
    Ok I need to make a calculator that does the requested operation to the number in the operand field. Right now I can't even get it to do anything. The interface is setup how I need it, but the action event doesn't seem to be working right?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    /*
    <applet code ="Calculator" width=300 height=200>
    </applet>
    */

    public class Calculator extends JApplet
    implements ActionListener
    {
    private JTextField operandText;
    private JTextField answerText;
    private JButton addButton;
    private JButton subtractButton;
    private JButton multiplyButton;
    private JButton divideButton;
    private JButton clearButton;

    private double total = 0;

    public void init()
    {
    setLayout (new GridLayout(7, 2, 5, 5));

    operandText = buildAField ("Operand", true);
    addButton = buildAButton("Add", new Color(155,155,255));
    subtractButton = buildAButton("Subtract", new Color(155,155,255));
    multiplyButton = buildAButton("Multiply", new Color(155,155,255));
    divideButton = buildAButton("Divide", new Color(155,155,255));
    clearButton = buildAButton("Clear", new Color(155,155,255));
    answerText = buildAField("Answer", false);

    }//init

    private JButton buildAButton (String buttonName,
    Color myColor)
    {
    JButton myButton = new JButton(buttonName);
    add (new JLabel(""));
    add (myButton);
    myButton.setBackground (myColor);
    myButton.addActionListener (this);
    myButton.setEnabled(true);
    return myButton;
    }

    private JTextField buildAField (String labelName,
    boolean editable)
    {
    JTextField myTextField = new JTextField();
    JLabel myLabel = new JLabel (labelName);
    myLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    add(myLabel);
    add(myTextField);
    myTextField.setHorizontalAlignment(SwingConstants.RIGHT);
    myTextField.setEditable(editable);
    if (!editable)
    myTextField.setFocusable(false);
    return myTextField;
    }

    public void actionPerformed (ActionEvent ae)
    {
    double operator;

    operator = Double.parseDouble(operandText.getText());

    if(ae.getSource () == addButton)
    total = total + operator;
    else if(ae.getSource () == subtractButton)
    total = total - operator;
    else if(ae.getSource () == multiplyButton)
    if (operator == 0)
    total = 0;
    else
    total = total * operator;
    else if(ae.getSource () == divideButton)
    {
    if (operator == 0)
    total = 0;
    else
    total = total / operator;
    }
    else if(ae.getSource () == clearButton)
    {
    clearMethod ();
    }
    answerText.setText (String.format ("%10.5", total));
    operandText.setText("");
    operandText.requestFocusInWindow();
    }

    public void clearMethod()
    {

    operandText.setText("");
    operandText.requestFocusInWindow();
    total = 0;
    answerText.setText (String.format ("%10.5", total));
    }

    }//class
     
  2. sprite

    sprite Active Member

    Joined:
    May 20, 2003
    Messages:
    2,967
    Likes Received:
    0
    Location:
    MI, USA
    Try this for starters... it should get you up and running and to the point you can do some further debugging... for example checking the operand & answer fields for zero length strings.

    EDIT - removed package declaration

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Calculator extends JApplet implements ActionListener {
    	private JTextField operandText;
    
    	private JTextField answerText;
    
    	private JButton addButton;
    
    	private JButton subtractButton;
    
    	private JButton multiplyButton;
    
    	private JButton divideButton;
    
    	private JButton clearButton;
    
    	private double total = 0;
    
    	public void init() {
    		getContentPane().setLayout(new GridLayout(7, 2, 5, 5));
    
    		operandText = buildAField("Operand", true);
    		addButton = buildAButton("Add", new Color(155, 155, 255));
    		subtractButton = buildAButton("Subtract", new Color(155, 155, 255));
    		multiplyButton = buildAButton("Multiply", new Color(155, 155, 255));
    		divideButton = buildAButton("Divide", new Color(155, 155, 255));
    		clearButton = buildAButton("Clear", new Color(155, 155, 255));
    		answerText = buildAField("Answer", false);
    
    	}// init
    
    	private JButton buildAButton(String buttonName, Color myColor) {
    		JButton myButton = new JButton(buttonName);
    		getContentPane().add(new JLabel(""));
    		getContentPane().add(myButton);
    		myButton.setBackground(myColor);
    		myButton.addActionListener(this);
    		myButton.setEnabled(true);
    		return myButton;
    	}
    
    	private JTextField buildAField(String labelName, boolean editable) {
    		JTextField myTextField = new JTextField();
    		JLabel myLabel = new JLabel(labelName);
    		myLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    		getContentPane().add(myLabel);
    		getContentPane().add(myTextField);
    		myTextField.setHorizontalAlignment(SwingConstants.RIGHT);
    		myTextField.setEditable(editable);
    		if (!editable)
    			myTextField.setFocusable(false);
    		return myTextField;
    	}
    
    	public void actionPerformed(ActionEvent ae) {
    		double operator;
    
    		operator = Double.parseDouble(operandText.getText());
    
    		if (ae.getSource() == addButton)
    			total = total + operator;
    		else if (ae.getSource() == subtractButton)
    			total = total - operator;
    		else if (ae.getSource() == multiplyButton)
    			if (operator == 0)
    				total = 0;
    			else
    				total = total * operator;
    		else if (ae.getSource() == divideButton) {
    			if (operator == 0)
    				total = 0;
    			else
    				total = total / operator;
    		} else if (ae.getSource() == clearButton) {
    			clearMethod();
    		}
    		answerText.setText(Double.toString(total));
    		// answerText.setText (String.format ("%10.5", total));
    		operandText.setText("");
    		operandText.requestFocusInWindow();
    	}
    
    	public void clearMethod() {
    
    		operandText.setText("");
    		operandText.requestFocusInWindow();
    		total = 0;
    		answerText.setText(Double.toString(total));
    	}
    
    }// class
    
    
     

Share This Page