Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calc with Events - Help !!  RSS feed

 
Shusha Tedi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ppl!
I really need help.I need to write a code for a calculator,something like the one used in windows.How do i do mathematical equations (for examp 3+2*5 - in the right order!) if the entered numbers are from buttons pressed (something to do with getActionCommand() ??).
Thanx sooo much,
Shusha.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most calculators that I know expect the user to enforce the order of execution by providing temporary memory areas that the user can store an intermediate result to. If they do NOT choose to use that option, then the calculator executes the operations in the order that they are entered, and displays the intermediate results along the way.
 
Ashwin Desai
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When a button is pressed, an action event is generated. Just trap that ActionEvent by providing an ActionListener. Check the component that generated the event( if (event.getSource() == button_1) ) and do the calculations based on the event source.

Hope this helps.
Ashwin.
 
Shusha Tedi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey again,
Thanx so much 4 ur help!
I dont understand 1 thing,the calc - when i press a button i generate an event which in this case presents what i pressed in the textfield.How can i use the buttons to do a mathematical equation ? like there type is Button,how do i do button_1 add button_2 equals(=) ... ?? (Without having to cast there type).
Thanx again,u guys r great.
Shusha Tedi.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!