Welcome Guest ( Log In | Click here to Register a free account now! )
Welcome to Bleeping Computer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.![]() ![]() |
Feb 11 2008, 08:04 AM
Post
#1
|
|
|
New Member ![]() Group: Members Posts: 2 Joined: 11-February 08 Member No.: 189,540 |
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(); } } |
|
|
|
Feb 11 2008, 08:23 AM
Post
#2
|
|
![]() Hail Groovicus! ![]() ![]() ![]() ![]() ![]() ![]() Group: Site Admin Posts: 5,985 Joined: 5-June 04 From: Vermillion, SD Member No.: 689 |
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. -------------------- |
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 7th October 2008 - 11:44 PM |