How can I set the Jbuttons in a preferred order?

Posted by Umzz Mo on Stack Overflow See other posts from Stack Overflow or by Umzz Mo
Published on 2012-04-15T17:18:51Z Indexed on 2012/04/15 17:29 UTC
Read the original article Hit count: 178

Filed under:
|
|

I have a grid of 30 buttons, and I want to have it from left to right then goes down, right to left, down again left to right. Basically the numbering would be as follow:

1 2 3 4 5 6 7 8 9 10

20 19 18 17 16 15 14 13 12 11

21 22 23 24 25 26 27 28 29 30

So if I have a piece on the button 10 and I instruct it to move up 2 bits it would land on 12 not 19.

Below is my Code:

//Creates the button using the loop, adds it into the panel and frame.
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3,10));
        JButton [] buttons = new JButton[30];
        for(int i=0;i<30;i++){
            buttons[i] = new JButton("label" + i);
            buttons[i].setBackground(Color.white);

        //Puts the player 1 piece on button 1,3,5,7,9 and player 2 piece on button 2,4,6,8,10
           if (i < 10) {
           if (i%2 == 0) {
             buttons[i].setIcon(piece1);
           } else {
             buttons[i].setIcon(piece2);
           }
        }
        panel.add(buttons[i]);
                    }

        frame.add(panel, BorderLayout.CENTER);

© Stack Overflow or respective owner

Related posts about java

Related posts about jbutton