Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

changing a jframe background color  RSS feed

 
Ranch Hand
Posts: 45
  • 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                      
 
Rancher
Posts: 2451
80
  • 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: 45
  • 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: 2451
80
  • 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: 45
  • 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!