Results 1 to 13 of 13
Thread: Calculator Help URGENT!
- 10-08-2012, 06:14 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Calculator Help URGENT!
Hi i'm working on a project for school and needs to be finished by Wednesday it docent give a result when u push the add subtract divide or mulitiply button. you operate this by putting the number in and then pushing the function.please help
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Calculator extends JFrame { JButton add ,subtract, multiply, divide; JTextField num1 , num2 ; JLabel result , enter1 , enter2 ; public Calculator () { setLayout (new GridBagLayout()); GridBagConstraints c = new GridBagConstraints (); enter1 = new JLabel ("lst: "); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; add(enter1,c); num1 = new JTextField(10); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 0; c.gridwidth = 3; add(num1 , c ) ; enter2 = new JLabel ("2nd: ") ; c.fill = GridBagConstraints.HORIZONTAL ; c.gridx = 0; c.gridy = 1; c.gridwidth = 1; add (enter2 , c ) ; num2 = new JTextField(10); c.fill = GridBagConstraints.HORIZONTAL ; c.gridx = 1; c.gridy = 1; c.gridwidth = 3; add (num2 , c ) ; add = new JButton ("+"); c.fill = GridBagConstraints.HORIZONTAL ; c.gridx = 0; c.gridy = 2; c.gridwidth = 1; add (add , c ) ; subtract = new JButton ("-") ; c.fill = GridBagConstraints.HORIZONTAL ; c.gridx = 1; c.gridx = 2; add (subtract , c ) ; multiply = new JButton ("*") ; c.fill = GridBagConstraints.HORIZONTAL ; c.gridx = 2; c.gridy = 2; add (multiply , c ); divide = new JButton ("/"); c.fill = GridBagConstraints.HORIZONTAL ; c.gridx = 3; c.gridy = 2; add (divide , c); result = new JLabel(""); c.fill = GridBagConstraints.HORIZONTAL ; c.gridx = 0; c.gridy = 4; c.gridwidth = 4; event a = new event () ; add.addActionListener(a) ; subtract.addActionListener(a) ; multiply.addActionListener(a) ; divide.addActionListener(a) ; } public class event implements ActionListener { public void actionPreformed (ActionEvent a) { double number1 , number2 ; try { number1 = Double.parseDouble(num1.getText()) ; } catch (NumberFormatException e) { result.setText("Illegal Data"); result.setForeground(Color.RED) ; return; } try { number2 = Double.parseDouble(num2.getText()) ; }catch (NumberFormatException e) { result.setText("illegal data"); result.setForeground(Color.RED); return; } String op = a.getActionCommand(); if (op.equals("+")) { double sum = number1 + number2 ; result.setText(number1 + "+" + number2 + "=" + sum ) ; result.setForeground(Color.RED) ; } else if (op.equals("-")) { double diff = number1 - number2 ; result.setText (number1 + "-" + number2 + "=" + diff) ; result.setForeground(Color.RED); } else if (op.equals("*")) { double factor = number1 * number2 ; result.setText (number1 + "*" + number2 + "=" + factor) ; result.setForeground(Color.RED); }else if (op.equals("/")) { if (number2 == 0) { result.setText("Cannot Divide by 0") ; result.setForeground(Color.RED); } else { double quotient = number1 / number2 ; result.setText(number1 + "/" + number2 + "=" + quotient); result.setForeground(Color.RED); } } } } public static void main (String args []) { Calculator gui = new Calculator (); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setVisible(true); gui.setSize(250,175); gui.setTitle("Event Night Product!"); } }Last edited by parkerp1000; 10-08-2012 at 06:32 PM.
- 10-08-2012, 06:22 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Calculator Help URGENT!
Please use [code] tags [/code] when posting code.
Few people will bother reading unformatted code.
For your actual problem, stick some debugging code (eg System.out.println("some meaningful comment")) in there to see what's happening.Please do not ask for code as refusal often offends.
- 10-08-2012, 06:33 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Re: Calculator Help URGENT!
I get this error when I push the funtion.
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
The type Calculator.event must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
at Calculator$event.actionPerformed(Calculator.java:8 1)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6382)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3275)
at java.awt.Component.processEvent(Component.java:614 7)
at java.awt.Container.processEvent(Container.java:208 3)
at java.awt.Component.dispatchEventImpl(Component.jav a:4744)
at java.awt.Container.dispatchEventImpl(Container.jav a:2141)
at java.awt.Component.dispatchEvent(Component.java:45 72)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4619)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4280)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4210)
at java.awt.Container.dispatchEventImpl(Container.jav a:2127)
at java.awt.Window.dispatchEventImpl(Window.java:2489 )
at java.awt.Component.dispatchEvent(Component.java:45 72)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:704)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:663)
at java.awt.EventQueue$2.run(EventQueue.java:661)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:677)
at java.awt.EventQueue$3.run(EventQueue.java:675)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 674)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
-
Re: Calculator Help URGENT!
This means that you're trying to run code that won't compile, something that you should avoid doing. Try compiling the code first and fixing the errors the compiler tells you about. Your current error seems to be due to your having a class that implements the ActionListener interface but that does not have all the methods required of this interface, namely a valid actionPerformed method. The fix is to give the class this method.
-
Re: Calculator Help URGENT!
Edit:
Aha!!!
Note that actionPreformed != actionPerformed. Look carefully.Java Code:public void actionPreformed (ActionEvent a) {
Consider using the @Override annotation before any and all overrided methods.
- 10-09-2012, 03:02 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
- 10-09-2012, 03:10 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
- 10-09-2012, 03:14 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 10-09-2012, 03:15 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 10-09-2012, 03:20 PM #10
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Re: Calculator Help URGENT!
I tried useing the @Overide before and it didn't work. if you can show me exactly were I need to add this that would be of great help and when you say
I'm not sure what you want me to do here either I tried to put the != there but im not sure what i need to do here.Note that. Look carefully.Java Code:actionPreformed != actionPerformed
- 10-09-2012, 03:40 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Calculator Help URGENT!
If someMethod is not in a parent of the class this has been written in then the compiler will complain, as it is an error.Java Code:@Override public void someMethod() { }
As for what I (and Fubarable) wrote, your code has:
Look at the spelling.Java Code:public void actionPreformed (ActionEvent a)
Compare that spelling to the API for ActionListener.
This is the reason the @Override annotation exists.Please do not ask for code as refusal often offends.
- 10-09-2012, 03:52 PM #12
Member
- Join Date
- Oct 2012
- Posts
- 6
- Rep Power
- 0
Re: Calculator Help URGENT!
it still dosent work i have tried every thing that you are saying
- 10-09-2012, 04:53 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
RMD Calculator
By java software in forum Java SoftwareReplies: 0Last Post: 10-08-2011, 05:42 PM -
Help in a calculator
By Ayannie in forum New To JavaReplies: 6Last Post: 01-04-2011, 08:21 PM -
[URGENT]SHA Encryption System...need urgent helps
By java_idiot in forum New To JavaReplies: 6Last Post: 05-02-2010, 10:04 AM -
help with calculator
By kalibballer in forum New To JavaReplies: 8Last Post: 04-01-2009, 12:57 PM -
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10-25-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks