I have created a Jframe/button with 10x10 grid. Each jbutton is apart of the grid. I am trying to how to affect each button pressed through JFrame/button, as I want to eventually make it into a battleships games.
frame.setLayout(new GridLayout(width,length));
grid=new JButton[width][length];
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
grid[x][y]=new JButton("("+x+","+y+")");
frame.add(grid[x][y]);
}
}
For example I am trying a basic piece of code to see if i can change the color of the Jframe to red by clicking it but it doesn't seem to be working.
public void actionPerformed(ActionEvent e){
if( e.getSource() instanceof JButton) {
((JButton)e.getSource()).setBackground(Color.red);
}
}
Anyone got any ideas?