aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Background color change help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Background color change help" Watch "Background color change help" New topic
Author

Background color change help

Rebecca Weber
Greenhorn

Joined: Apr 06, 2011
Posts: 3
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:

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4637
    
    5

Change the background of the JFrame's contentPane instead. To understand why, read the API for JRootPane.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Background color change help