• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple GUI - button not responding to clicks

 
John Deegan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, all. This is my first post, and I am a bit new at this, as I am sure you will be able to tell soon. This code is loosely based on an example problem in Head First Java. It is a GUI with a button and a circle that should change color when the button is clicked. The code compiles and runs, but the problem is that nothing happens when I click on the button. I think it has to do with repaint(), but flipping through the API and looking online has not helped me much. Any ideas?

 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you want to repaint the JPanel, not the JFrame, isn't that right?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> nothing happens when I click on the button. I think it has to do with repaint()

no, the color never changes (0,0,0)

gr = (int) Math.random() * 256;
is (int) Math.random() = < 1.00, so int value is 0
0 * 256 = 0

change to this
gr = (int) (Math.random() * 256);
ditto for the others
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and, Welcome to the Ranch!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might be better to use a Random object, because that has a more specific method for getting pseudo‑random ints.
 
John Deegan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael's suggestion ended up fixing it. Thanks guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic