1

Basically I am trying to make a Lightout game! I want to make an array of JButtons so I can keep track of the index of each of them (the reason being the state of each button is dependant on the state of the others)

so far I have:

JPanel panel = new JPanel();
    setTitle("Memory");
    setContentPane(panel);
    setPreferredSize(new Dimension(300, 300));
    panel.setLayout(new GridLayout(5,5));


    JButton[][] buttons = new JButton[5][5] ;
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++) {
          buttons[i][j] = new JButton();

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
        }

But this isnt working how I expect it to. I just got a big blank JFrame when I run this. Any help would be appreciated

5
  • 4
    Where did you add all these buttons to frame in your code Commented Apr 7, 2016 at 3:58
  • I was under the impression that buttons[i][j] = new JButton(); was the way to add buttons? Im really sorry, I'm kinda a noob at this Commented Apr 7, 2016 at 4:02
  • @Elchapo your for loop is bad .how much time you call pack and setvisible Commented Apr 7, 2016 at 4:06
  • @FastSnail Thanks! Ive fixed it Commented Apr 7, 2016 at 4:13
  • @Elchapo that's why it's discourage to omit curly brackets in for loop. there is a higher change to make a mistake .so use curly brackets to wrap if blocks Commented Apr 7, 2016 at 4:17

1 Answer 1

5

The attached code should fix it. You were creating button, but not adding it to the JFrame. I have edited the code to add action listener which accesses the id of the JButton and displays it , when you click it.

public class CodeSample extends JFrame {

private static final long serialVersionUID = -8134989438964195251L;

public CodeSample() {
    JPanel panel = new JPanel();
    setTitle("Memory");
    setContentPane(panel);
    setPreferredSize(new Dimension(300, 300));
    panel.setLayout(new GridLayout(5, 5));
    ButtonListener listener = new ButtonListener();

    JButton[][] buttons = new JButton[5][5];
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            buttons[i][j] = new JButton();
            buttons[i][j].setBackground(Color.black);
            buttons[i][j].putClientProperty("id",
                    String.valueOf(i).concat(String.valueOf(j)));
            buttons[i][j].addActionListener(listener);
            panel.add(buttons[i][j]);
        }
    }

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

public static class ButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(((JButton) e.getSource())
                .getClientProperty("id"));
    }

}

public static void main(String[] args) {
    new CodeSample();
}
} 
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks a ton! that makes sense now! just to be clear, this definitely had to be a Matrix array correct? and the button at index [0][0] is going to be top left button?
@Elchapo yes that`s right [0][0] is going to be top left button. The can be implemented in multiple ways. Matrix array is a good choice for this case.
This implementation is a matrix, but JButton (like any object) need not necessarily be stored in a two-dimensional array. One could also have a one-dimensional array of buttons.
@ingrid Thanks a bunch! I can set the background on the buttons with buttons[i][j].setBackground(color.black); correct? (Im on a mac if that makes a difference..)
@Elchapo spot on. You can add back ground by calling buttons[i][j].setBackground(color.black);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.