Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-05-2006, 05:30 PM   #1 (permalink)
black_orc
Registered User
 
Join Date: Aug 2006
Posts: 1
black_orc is on a distinguished road
Question Calculator code

Can anyone look at my calculator code... I get a trouble when compiling it.
Heres the code: p.s. im new to java so please be gentle...


Code:
import java.awt.*;
import java.awt.event.*;



public class Calculator extends Panel implements ActionListener {
  private TextField display = new TextField("0");

  private String buttonText = "789/456*123-0.=+";

  private double result = 0;

  private String operator = "=";

  private boolean calculating = true;

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

    display.setEditable(false);
    add(display, "North");

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

    for (int i = 0; i < buttonText.length(); i++) {
      Button b = new Button(buttonText.substring(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(".")) {
      if (calculating)
        display.setText(cmd);
      else
        display.setText(display.getText() + cmd);
      calculating = false;
    } else {
      if (calculating) {
        if (cmd.equals("-")) {
          display.setText(cmd);
          calculating = false;
        } else
          operator = cmd;
      } else {
        double x = Double.parseDouble(display.getText());
        calculate(x);
        operator = cmd;
        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 void main(String[] args) {
     Frame frame = new Frame();
    frame.setTitle("Calculator");
    frame.setSize(200, 200);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = frame.getContentPane();
    contentPane.add(new Calculator());
    frame.show();
  }
}
black_orc is offline   Reply With Quote
Old 08-06-2006, 03:30 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
What's the error message you get when you compile it?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 05-09-2008, 12:22 AM   #3 (permalink)
@script
Code Monkey
 
Join Date: May 2008
Posts: 36
@script is on a distinguished road
you might have using the deprecated method. try to refine your code in the section of:

Container contentPane = frame.getContentPane();
@script is offline   Reply With Quote
Old 05-14-2008, 12:58 PM   #4 (permalink)
Rotkiv
Code Monkey
 
Rotkiv's Avatar
 
Join Date: Apr 2004
Location: Silicon Valley, CA
Posts: 59
Rotkiv is on a distinguished road
Send a message via AIM to Rotkiv Send a message via MSN to Rotkiv
Thumbs up

use JFrame and JPanel instead of just normal Frame and Panel by using
Code:
import javax.swing.*;
and get rid of container, in the end your main function should look like this.
Code:
public static void main(String[] args) {
     JFrame frame = new JFrame();
    frame.setTitle("Calculator");
    frame.setSize(200, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new Calculator());
    frame.setVisible(true);
}
JFrame has the .setDefaultCloseOperation() method so that takes the place of your listener and 5 lines of code.

with all those changes it works fine for me and looks great. GJ.
Rotkiv is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 08:33 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting