Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes Simple GUI - button not responding to clicks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Simple GUI - button not responding to clicks" Watch "Simple GUI - button not responding to clicks" New topic
Author

Simple GUI - button not responding to clicks

John Deegan
Greenhorn

Joined: Nov 27, 2012
Posts: 4
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

But you want to repaint the JPanel, not the JFrame, isn't that right?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Oct 16, 2003
Posts: 1076
    
  10

and, Welcome to the Ranch!


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Might be better to use a Random object, because that has a more specific method for getting pseudo‑random ints.
John Deegan
Greenhorn

Joined: Nov 27, 2012
Posts: 4
Michael's suggestion ended up fixing it. Thanks guys!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple GUI - button not responding to clicks
 
Similar Threads
calling repaint in an ActionListener
problem with simple swing GUI program
GUI Example (HFJ)
why does this code fail?
need help with my code