Reputation: 77
grid=new JButton[width][length];
for(y=0;y<length;y++){
for(x=0;x<width;x++){
final Border border=new LineBorder(Color.red, 5);
grid[x][y]=new JButton(" ");
grid[x][y].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
((JButton)e.getSource()).setBorder(border);;
System.out.println("Where do you want to move this piece");
}
});
frame.add(grid[x][y]);
}
}
grid[1][1]='R';
I am trying to get the first spot of the grid to say the letter R as in Rook; I am not sure how I can label specific JButton in a grid. Please help me... I am trying to make a GUI chess game.
Upvotes: 0
Views: 187
Reputation: 159784
Use methods instead of =
to set component properties in Swing
grid[0][0].setText("R");
Upvotes: 4
Reputation: 285405
This won't compile:
grid[1][1]='R';
Because you're trying to assign a char to a JButton. Instead understand that the grid holds JButtons, and call the JButton methods:
grid[0][0].setText("R");
Also, you don't want to do this:
System.out.println("Where do you want to move this piece");
It's a bad idea to combine println statements and GUI's (unless you're debugging). Rather, show that String in a JLabel.
Upvotes: 4