Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Calculator help needed - Button Listener  RSS feed

 
Rodger Clarke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create a button listener for a calculator so for instance when I press "-", it will hear it and then from there other things will happen. I have a support calculator class that calculates stuff, called Calculator, and am referencing it at the start of the `CalculatorPanel` method, but under the `ActionListener` portion is where I am having problems.

I don't know how to create a button listener to get my code to work, as the one that I have in the ButtonListener part at the moment doesn't work. I get this error: "cannot find symbol symbol : method addItemListener(CalculatorPanel.ButtonListener) location: class Calculator"

I'm stuck and need help!


   
  
 
Dave Tolls
Rancher
Posts: 2914
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have pointed out the line you had the problem with.
Anyway, it's this one:


What are you trying to do here?
The Calculator class has no addItemListener method.
It also makes no sense to me to add an ActionListener to something that isn't a Swing component.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you using an item listener? Use action listeners. You got off to a good start with that array, so you can use that text to add an action listener to each number button. Assuming you are using decimal numbers, the listener needs to multiply a number by ten and add the button's numeric value. (Slightly different if you are using floating‑point numbers.)
It is unfortunately different for operators; you may have to take the operators out of that array and make a separate array, adding the buttons separately. Or try an enumerated type, which you can get an array out of. Look through the Java® Language Specification (=JLS) section. The JLS can be difficult to understand, but there might be something very helpful for your current project in that section I think you will find useful learning how to get from "+" to a PLUS enum element.
 
Rodger Clarke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get it to function as a calculator would, using the Calculator class to do the computation and the Calculator Panel class as the interface for it. Currently if I were to input "7" it just says "you pressed 7" and I can't figure out how to get it to function as a calculator.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
n that case you probably need a calculator running at the command line/terminal with no GUI. Add the GUI after you have got that working.
 
Rodger Clarke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could this question get deleted please? I gave up on this code and did another one instead.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rodger Clarke wrote:Could this question get deleted please? . . .
We only delete posts which have something seriously wrong with them; your posts might be of use to future readers.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!