0

Is there any way to assign random numbers to JButtons? I'm creating a game where the player must click the draw button to shuffle the numbers on the cards. I want the numbers to be 1, 2, 3, 4, 5, 6, 7, 8, and 9.

picture

How do I do this?

GreenJames
  • 21
  • 1
  • 8
  • 2
    Possible duplicate of [Generating random integers in a specific range](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range) – rdonuk Mar 23 '16 at 14:09
  • 3
    What is your problem exactly? How to generate random numbers 1 to 9 or how you set the Button with the number? – MrT Mar 23 '16 at 14:09
  • I want to first generate the integers, then randomly assign them to a button giving them a random number for every draw. – GreenJames Mar 23 '16 at 14:15
  • then, put this aclaration in your question. – hcarrasko Mar 23 '16 at 14:25

1 Answers1

1
Random r = new Random();
r.nextInt(max - min + 1) + min

This will generate random numbers for you within bounds (inclusive). So, your min is 1 and max is 9.

to set text on JButton there is a method available (https://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setText(java.lang.String))

Scott Tomaszewski
  • 1,009
  • 8
  • 27