简体   繁体   中英

Which button was clicked in Java?

I'm working (failing pretty bad so far tbh) on a battleship game for my high school Java programming class. So far I have the game board for both the computer and the player and I have generated the ships for the computer and made sure the player can sink them. Both of the boards have a grid layout, and each of them is assigned to a 2-D array for both the player and the computer and is part of the specific Grid Layout (to be honest I don't really understand a lot of the code because it was supplied by our teacher, so I can't really tell which parts are relevant and which aren't - which is also why I am not posting any of my code).

What I want to do now is let the Player place their ships by letting them pick a starting place by clicking on the board.

inside of a for loop
            1: a button in buttonsPlayer is clicked
            2: when a button is clicked, the two coordinates are calculated and stored as x, y coordinates 
            3: a ship is generated with the starting coordinates of x, y

I know how to generate a ship with random x and y starting coordinates as I have done that before. Is there a way to get a button's number in an array after clicking on a button?

(I did read through like 5 other threads on here that seemed to ask the same question, but I don't really get any of the answers)

You could create a class that extends JButton and adds more functionality to a JButton like so:

public class BoardPiece extends JButton{
     private int x,y;
     //rest of class including getters and setters
}

This will bring all the functionality of JButton along with it and you can add in essentially meta-data about each square.

Then in your event listener you would be able to just call .getX() and .getY() like so:

boardPiece.addActionListener((e)->{
    BoardPiece clicked = (BoardPiece)e.getSource();
    int x = clicked.getX();
    // and so on

});

A JButton has an 'action command' which allows you to put information in it to distinquish it from other buttons.

using

JButton.setActionCommand
JButton.getActionCommand

So if you encode the coordinates of that button in that string, in your actionListener you can

JButton b = (JButton) eventListener.getSource();

and

String cmd = b.getActionCommand();

and then process that cmd string to figure out where on the board you are.

You can give a JButton an ActionCommand, eg

    JButton button1 = new JButton("Button 1");
    button1.setActionCommand("Button1id");`

Then if you implement an ActionListener to listen for the buttonpress, you can write code like

    @Override
    public void actionPerformed(ActionEvent ae) {
        String buttonid = ae.getActionCommand();
    }

By checking the value of buttonid , you will know which button was pressed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM