• Post Reply Bookmark Topic Watch Topic
  • New Topic

Some help needed with ActionListener  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am on the last assignment of this summer session and I can get most of the program to compile, except for the last section which is using ActionListener...

Basically, I need this program to take the input from one text field, convert to a double, do some math, and then convert that result back to text and print that text into another text field when the user clicks a button. Here is a copy of the source code. Once again, I could use any help offered.

I've attached a screenshot of the compiler errors.
Capture.PNG
[Thumbnail for Capture.PNG]
Compiler errors
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some Java error messages can be a little obscure, but that is not one of them.
You are implementing an interface but not providing an implementation of all of the methods in that interface.
The error message tells you which interface it is referring to and the name and argument list of the method that you haven't implemented.
 
Saloon Keeper
Posts: 8109
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Antonio,

Please put code in [code] tags the next time. I did it for you this time.

The prompt tells you exactly what's wrong:

1) Your class does not override the actionPerformed() method.
2) fTemp does not have a setText() method. Consider the type of fTemp.
3) You cannot dereference doubles, meaning you can't use the dot operator on c. The Double class has a way to convert a double to a String.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second third (I missed the second one) error is a little more opaque. It basically means you can't call a method on a primitive variable. Primitives (int, long, float, etc) don't have any methods - only objects have methods.
 
Stephan van Hulst
Saloon Keeper
Posts: 8109
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some other issues you should consider:
  • Your "constructor" isn't really a constructor. It is a method named Window() that returns a double.

  • Do not name your class Window. Window is already a well known class in the java.awt package. You should always avoid giving classes the same name as those in the standard API (or at least the most commonly used packages).

  • Don't let your frames implement ActionListener. This is a commonly occurring anti-pattern that is taught in a lot of tutorials and classes. Instead, you should make one listener instance per action, like so:
    Later, when you are familiar with this approach, and you learn about anonymous classes, you can do this instead:
    Or in Java 8:
    My personal favorite is this approach though:
    In the last example, you can make multiple buttons with the same action, and you can even enable and disable them all at the same time by simply manipulating the action instance.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!