This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

changing a jframe background color  RSS feed

 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I'm pretty new to java and could use some help.  I have a program that is running fine, but the last thing I need to do is have the background of the Jframe change every time a button is clicked.  So I've added a color command to my button and the background changes to green, no problem.  But I don't know how to finish the code by overriding that color if the button is clicked.  Here is the pertinent code:

     

Any suggestions or pointers would be greatly appreciated. 

Thanks                      
 
Piet Souris
Rancher
Posts: 2015
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One simple way would be to have a method getColor(), something like

and in your ActionListener, in stead of using Color.GREEN, use getColor().

edit: read for 'ActionListener'  'MouseListener'
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet,

Thanks for the response!  When I added in your code, for lines 4, 5, & 6, I get an "illegal start of expression" error.  Thoughts?
 
Piet Souris
Rancher
Posts: 2015
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, these dots were meant to be replaced by correct syntax. I did not supply completely working code, because I was afraid I had given already too much information.
Look at how I defined the variable 'red'. you should do something equal to the variables green and blue.
Then have a look at the API of the Color class, to see how you can create a new Color (API).

Tyy it, and let us know whether you succeeded!
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate that,  I'll give it a try and let you know!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!