-1

how would i randomly position a button on a certain portion of the frame. I have tried to set two variables x and y which are random but it doesn't work, the button disappears i'm guessing it is positioned off the screen.

Thi sis what i have tried:

int y = ran.nextInt(0 - frame.getHeight());
int x = ran.nextInt(0 - frame.getHeight());

I am also getting an error 'AWT-EventQueue -0'

Thanks

Rachel
  • 23
  • 1
  • 2
  • 8

2 Answers2

2

You have a negetive value in the bracket.

int y = ran.nextInt(frame.getHeight());
int x = ran.nextInt(frame.getWidth());
Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
  • 'AWT-EventQueue -0' When i add in a number before the frame.getHeight() and frame.getWidth() the error is not longer there but the button doesn't appear on the screen. – Rachel Apr 18 '13 at 20:46
  • Sorry what is the stack trace – Rachel Apr 18 '13 at 20:50
2
  1. not frame.getHeight() but frame.getContentPane.getHeight()

  2. required to use AbsoluteLayout for container where is JButton placed

  3. add (re)validate and repaint to container where is JButton placed

  4. use JPanel as container for JButton

mKorbel
  • 109,525
  • 20
  • 134
  • 319