1

Im displaying a 8x8 grid of pictures for a new game and these pictures are places randomly using a two dimensional array and math.random()

There is also a "new game" button I want the user to be able to refresh the page without opening a new GUI enter image description hereand re displaying the grid aswell.

This is my method for displaying the grid

for (int r = 0; r < ShinyButtons.ROWS; r++){
        for (int c = 0; c < ShinyButtons.ROWS; c++) {

              newbuttonTable[r][c] =  new JButton(icons[(int) (Math.random()*7)]);
              newbuttonTable[r][c].setLocation(10+c*69, 10+r*69);
              newbuttonTable[r][c].setSize(69, 69);
              add(newbuttonTable[r][c]);


    }
Bauer
  • 159
  • 4
  • 13
  • 1
    Consider using a `GridLayout(...)` instead of manually placing the buttons with `setBounds`. Apart from that: You can remove all buttons from this panel by calling `removeAll()` (but am not sure whether this is what you are asking for) – Marco13 Feb 28 '14 at 16:56

1 Answers1

0

"I want the user to be able to refresh the page without opening a new GUI"

You should use a CardLayout for this functionality. You can "stack" panels and navigate through them with methods like show(p, "aCertainPanel"), next(), previous().

See How to use CardLayout and see a simple runnable example here.

Also as Marco13 pointed out, avoid using null layouts and setting location and sizes manually. There many problems that arise with this. For your particular situation, it looks like GridLayout would be the perfect layout for you. See How to use GridLayout as well as others at Laying out Components Within a Container

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720