How do I create individual button names in an array?
I'm trying to make a game of battleship, I'm in control of the boards. How do I make an array that will allow each button to return a position. For storing the ships and the hits and things. I want to make each i开发者_运维百科ndividual buttons but I'm not sure how to go about doing this.
You could extend the button class:
public class BattleShipButton extends JButton {
private Coordinate coords;
public BattleShipButton(Coordinates coords) {
this.coords = coords;
setPreferredSize(new Dimension(20, 20));
}
public Coordinates getCoordinates() {
return coords;
}
}
Then, you could instantiate the buttons in a loop and pass in the correct coordinates.
BattleShipButton[][] buttons = new BattleShipButton[boardWidth][boardHeight];
for(int i = (int)'a'; i < boardWidth; i++){
for(int j = 0; j < boardHeight; j++) {
buttons[i][j] = new BattleShipButton(new Coordinate((char)i, j));
}
}
Then each button will have the correct coordinates that you can get at using getCoordiantes()
.
Just do them in a Matrix, if you wanna know their positions, their index, should be enough.
If you wanna be more specific, you can create your own buttons and make then have a function which return, that position
Create a new class that extends AbstractAction. Give it attributes for the coordinates and create new instances and pass in to the JButton's constructor when you create the buttons.
In doing so you will override the actionPerformed method of Action to allow you button click event to make use of those coordinates all nicely packaged in the same class.
Just do not if this can help you but better than nothing i used form application go ahead take a look
You can create an array like this
Button[] Barray = new Button[32,32];
than you will have to fill this array
for (int i = 0; i <= a - 1; i++)
{
for (int j = 0; j <= b - 1; j++)
{
Barray (i, j) = new Button(); // here you create a dynamic button
Barray (i, j).Location = new System.Drawing.Point(x, y); // here you locate the button in to your box or in a similar container
Barray (i, j).Size = new System.Drawing.Point(23, 23); // in this line you resize your button
Barray (i, j).Name = i + j; // in this lie you rename your button so that you will be able to reach your button and know what is its location
Barray (i, j).Click += Button_Click; // in this line you will add the button_click event to your dynamic buttons
Form1.box.Controls.Add(dizi(i, j)); // and this line adds your button to your container
x += 23; // in this line i am increasing the x location so i the buttons will not be placed at the same x location
}
x = 0; // this line i ll make x zero that means i m finis previous line and i ll start to place buttons on another line
y += 23; // this line i m increasing the y location to move the next line
}
after these codes i ll need to create an event to catch the button click event:)
private void button_Click(object sender, System.Windows.Forms.MouseEventArgs e)
{
//here you can use e eventargs reach your buttons name so you can do anything you want :)
}
hope this helps you
精彩评论