Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

cannot find symbol for button  RSS feed

 
Shailendra Phadke
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.

0Below code is not working. It says "cannot find symbol" for btn1"

 
Carey Brown
Saloon Keeper
Posts: 2537
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'btn1' is declared inside the constructor so is therefore a local variable within the constructor and is not visible in other methods. You'll need to declare btn1 as a field outside of the constructor, but still initialize it within the constructor.
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest the real problem is incorrect use of the ActionListener interface. One of at least three design problems I can see.
  • 1: Don't use AWT, which was superseded by Swing over 15 years ago. Since Swing is probably on it way out, maybe you should use JavaFX instead of Swing.
  • 2: You are extending display classes, which is probably a bad idea. You can add Components to a frame easily without subclassing the frame (see below).
  • 3: You are making the display class implement ActionListener which is an abomination against object‑oriented programming, even though you will see it in the books. Use a separate class which implements ActionListener, whether public private or anonymous. In Java8 you would use a λ rather than an anonymous class. That way there will be no actionPerformed method in the display class, and no need for == btn1 and the compiler error will disappear.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!