简体   繁体   English

在Java中单击了哪个按钮?

[英]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. 我正在为我的高中Java编程课程开发一款战舰游戏(到目前为止还很糟糕)。 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. 我知道如何像以前一样用随机的x和y起始坐标生成一艘船。 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) (我确实像这里的其他5个线程一样通读了,似乎在问同样的问题,但是我没有真正得到任何答案)

You could create a class that extends JButton and adds more functionality to a JButton like so: 你可以创建一个类extends JButton ,并增加了更多的功能到JButton ,像这样:

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. 这将带来JButton所有功能,并且您可以添加有关每个正方形的本质元数据

Then in your event listener you would be able to just call .getX() and .getY() like so: 然后,在事件监听器中,您可以像这样调用.getX().getY()

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. JButton具有一个“操作命令”,可让您在其中添加信息以区别于其他按钮。

using 运用

JButton.setActionCommand
JButton.getActionCommand

So if you encode the coordinates of that button in that string, in your actionListener you can 因此,如果您在该字符串中编码该按钮的坐标,则可以在actionListener中

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. 然后处理该cmd字符串以弄清楚您在板上的位置。

You can give a JButton an ActionCommand, eg 您可以给JButton一个ActionCommand,例如

    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 然后,如果实现ActionListener来监听按钮按下,则可以编写如下代码

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

By checking the value of buttonid , you will know which button was pressed. 通过检查buttonid的值,您将知道按下了哪个按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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