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
Go Back   Code Forums > Application and Web Development > Java
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
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,114
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
Recruit
 
Join Date: May 2008
Posts: 7
@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
Reply


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

vB 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 04:06 PM.


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





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle