Results 1 to 2 of 2
  1. #1
    trtoy is offline Member
    Join Date
    Dec 2009
    Rep Power

    Default Basic Calculator

    So as you can see I am very new to this...

    I am getting an error while using textpad. The error is "Desktop\ illegal character: \29"

    I need to add other functions but I thought I would try to run it first and it doesn't even work with just the addition. Here is thy code.

    Thanks people!

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class BasicCalculator extends JFrame{
    private static final int FRAME_WIDTH = 150;
    private static final int FRAME_HEIGHT = 120;

    // Keeps track of the current operation (subtract, add, etc)
    private static final int NO_OPERATION = 0;
    private static final int ADDITION = 1;
    public static int operation = NO_OPERATION;

    public static JTextField textFieldDisplay;
    public static double Value1 = 0; // holds the value before the operation

    public static void main(String[] args) {
    // Set up the user interface
    JFrame frame = new JFrame();
    JPanel buttonPanel = new JPanel();

    // create two buttons, plus and equal and a text box for answers
    textFieldDisplay = new JTextField(10);
    JButton buttonPlus = new JButton(" + ");
    JButton buttonEqual = new JButton(" = ");

    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setTitle("Basic Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    // called when the equal sign '=' is pressed
    class EqualSignListener implements ActionListener {

    public void actionPerformed(ActionEvent event)
    double Value2 = Double.parseDouble(textFieldDisplay.getText());
    if (operation == ADDITION) {
    // plus sign pressed before the equal sign
    Value2 += Value1;
    // Convert from a answer to a string
    Double answer = new Double(Value2);
    textFieldDisplay.setText( answer.toString() );
    // Reset the operation to show no current operation
    operation = NO_OPERATION;

    // called when a plus sign '+' is pressed
    class PlusSignListener implements ActionListener {
    public void actionPerformed(ActionEvent event)
    Value1 = Double.parseDouble(textFieldDisplay.getText());
    operation = ADDITION;

    // Add the methods that will be called when these buttons are pressed
    ActionListener plusSignListener = new PlusSignListener();

    ActionListener equalSignListener = new EqualSignListener();


  2. #2
    trtoy is offline Member
    Join Date
    Dec 2009
    Rep Power

Similar Threads

  1. need help with my calculator
    By semoche in forum AWT / Swing
    Replies: 6
    Last Post: 12-04-2009, 10:16 PM
  2. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 06:00 AM
  3. Basic Calculator
    By Rose88 in forum Java Applets
    Replies: 3
    Last Post: 06-25-2009, 12:34 AM
  4. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 12:57 PM
  5. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts