This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
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

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Background color change help" Watch "Background color change help" New topic

Background color change help

Rebecca Weber

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

Joined: May 03, 2008
Posts: 5098

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

Joined: Oct 27, 2005
Posts: 20202

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Background color change help
It's not a secret anymore!