• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Background color change help

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Writing a guessing game program. The user is to guess a number between 1 - 1000. Every time the user guesses too low, the background color of the JFrame is to change to blue, if the user guesses too high, it changes to red. I cannot get the background color to change at all. Everything else seems to running ok with the program. Any help would be appreciated. Thanks. Here is my code:

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change the background of the JFrame's contentPane instead. To understand why, read the API for JRootPane.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't call setBackground from paintComponent. That will trigger another repaint which will call paintComponent again. Instead of using your variable "background" just call setBackground in your event handlers.

And you're calling super.paintComponents instead of super.paintComponent. Because both methods exist you didn't get a compiler error but you should definitely fix that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic