Help - Search - Members - Calendar
Full Version: Help Programming Calculator.java
BleepingComputer.com > Software > Programming
   
eduardo0
hi im eduardo0 i have problem to compile my project it is java file please help me to do this this is my codes but i have many errors in this codes please help me :D
CODE
Calculator.java:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class Calculator extends JPanel implements ActionListener
{
  private JtextField Display = new JtextField("0");
  private String Button = "789/456*123-0.=+";
  private double result = 0;
  private String operator = "=";
  private boolean calculating = true;
  private boolean decimal = false;

  public Calculator()
  {
  setLayout(new BorderLayout());

  display.set.Editable(false);
  add(display, "North");

  JPanel p = new JPanel();
  p.setLayout(new GridLayout(4, 4));

  for (int i = 0; i < button.length(); i++)

  {
  JButton p = new JButton(button.subsstring(i, i + 1));
  p.add(B);
  b.addActionListener(this);

  }

  add(p, "center");
  }

  public void actionPerformed(ActionEvent evt)
  {
  String cmd = evt.getActionCommand();
  if ('0' <= cmd.charAT(0) && cmd.charAT(0) <= '9' || cmd.equals("."))
  {
           System.out.println("Entered Here");

           if (calculating){
                System.out.println("Entered Calculating..." + cmd.
charAT(0));
           display.setText(cmd);

        }
       else
          display.setText(display.getTest() + cmd);
       calculating = false;
     }

     else
     {
       if (calculating)
       {
         if (cmd.equals("-"))
         {
            display.setText(cmd);
           calculating = false;
         }
         else
             operator = cmd;
       }
         else
       {
        try{
                 double x = Double.parseDouble(display.getText());
                 calculate(x);
        operator = cmd;
        calculating = false;
                }catch(NumberFormatExeption e){
                    display.setText("Illegal Number Format");
                    calculating = false;

                }
                calculating = true;

        


           }
         }
          }

          private void calculate(double n)
          {
          if (operator.equals("+"))
                result += n;
          else if (operator.equals("-"))
                result -= n;
          else if (operator.equals("*"))
                result *= n;
          else if (operator.equals("/"))
                result /= n;
          else if (operator.equals("="))
                result = n;
          display.setText("" = result);
           }

           public static viod main(String [] args)
           {
          JFrame Frame = new JFrame();
          frame.setTitle("Calculator");
          frame.setBounds(250, 250, 200, 200);
          frame.addWindowsListener(new WindowAdapter();
          {  
            public void windowClosing(WindowEvent e);
          
              } System.exit(0);
            
         }
        }

        Container contentPane = frame.getContent.Panel();
        ContentPanel.add(new Calculator());
        frame.show();
         }
  }
groovicus
Just from a quick glance, you have void spelled wrong in the man method. Specifically, what errors are you having?? You're code is not formatted well, and there are no comments as to what you think each step should be doing.

EDIT: that Calculator.java business at the top is wrong also. I assume that when you pasted in the code that you missed the slashes.

EDIT@: I just took another quick look...where are you getting this code from? There is no such thing as a JtextFiels, you want a JTextField. You declare an array of Button labels, but you theen try to use button instead. There is no set.Editable method. You want setEditable. You use the statement p.add(B);, but you do not declare anything to be B. You try to add an action listener to b.addActionListener(this), but there is no object named b.

That's only the first 20 lines. You need to go back and start over and correct all of your spelling errors, maybe start with a much simpler project.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.