简体   繁体   English

单击时Java-FX接收Shape-Array的索引

[英]Java-FX Receive Index of Shape-Array when clicked

I am trying to build a battle ship application. 我正在尝试构建战舰应用程序。 So far I managed to display both fields for the player and the enemy. 到目前为止,我设法同时显示了玩家和敌人的两个字段。 A field consists of 10 x 10 Rectangles - 10 HBox'es in one VBox. 一个字段由10 x 10个矩形-一个VBox中的10个HBox组成。 This is the method that creates me a field: 这是为我创建字段的方法:

private Rectangle[][] field;    
public VBox CreateField(){
        VBox vbox = new VBox(2);
        for(int i = 0; i < this.columns; ++i){
            HBox hbox = new HBox(2);
            for(int j = 0; j < this.rows; ++j){
                this.array[j][i] = 0;
                this.field[i][j] = new Rectangle(this.height*j, this.width*i, this.height, this.width);
                this.field[i][j].setStroke(Color.BLACK);
                this.field[i][j].setFill(Color.LIGHTGRAY);                
                hbox.getChildren().add(this.field[i][j]);
            }
            vbox.getChildren().add(hbox);
        } 
        return vbox;
    }

This is the result: 结果如下:

在此处输入图片说明

I need to receive the Index of a Rectangle when the user clicks one of it. 当用户单击其中一个时,我需要接收一个矩形的索引。

Can you guys give me an example / code snipped how to accomplish my problem? 你们能给我一个例子/代码如何完成我的问题吗?

Lol, got it faster work than I thought. 大声笑,让它比我想象的更快。 Here is my solution ( How can I get the indexes of each button clicked for my program? ) 这是我的解决方案( 如何获得程序单击的每个按钮的索引?

private EventHandler<? super MouseEvent> createTileHandler(int x, int y) {
    return event -> tileHandler(x, y);
}
private void tileHandler (int x, int y){
    System.out.println(String.format("Clicked tile at (%d,%d)", x, y));
}
public VBox CreateField(){
    VBox vbox = new VBox(2);
    for(int i = 0; i < this.columns; ++i){
        HBox hbox = new HBox(2);
        for(int j = 0; j < this.rows; ++j){
            this.array[j][i] = 0;
            this.field[i][j] = new Rectangle(this.height*j, this.width*i, this.height, this.width);
            this.field[i][j].setStroke(Color.BLACK);
            this.field[i][j].setFill(Color.LIGHTGRAY);      
            this.field[i][j].setOnMouseClicked(createTileHandler(i,j));
            hbox.getChildren().add(this.field[i][j]);
        }
        vbox.getChildren().add(hbox);
    } 
    return vbox;
}

This returns: 返回:

Clicked tile at (3,2)
Clicked tile at (3,2)
Clicked tile at (4,4)
Clicked tile at (3,0)
Clicked tile at (8,8)
Clicked tile at (9,9)
Clicked tile at (0,0)
Clicked tile at (0,1)
Clicked tile at (0,2)

When I click one Shape. 当我单击一个形状时。 Apologizing for this questions .. 为这个问题表示歉意..

I recommend using a GridPane instead of HBox es and a VBox . 我建议使用GridPane代替HBoxVBox You could copy the loop indices to final local variables to access them from a anonymus EventHandler class/lambda expression created inside the loop body or you could use GridPane.getRowIndex / GridPane.getColumnIndex on the GridPane children: 您可以复制循环指数,以final局部变量从anonymus访问这些EventHandler的循环体中创建的类/ lambda表达式或者你可以使用GridPane.getRowIndex / GridPane.getColumnIndexGridPane孩子:

public GridPane CreateField(){
    GridPane grid = new GridPane();
    grid.setVgap(2);
    grid.setHgap(2);
    EventHandler<MouseEvent> handler = evt -> {
        Node source = (Node) evt.getSource();
        int row = GridPane.getRowIndex(source);
        int column = GridPane.getColumnIndex(source);
        ...
    };
    for(int i = 0; i < this.columns; i++){
        for(int j = 0; j < this.rows; j++){
            Rectangle rect = new Rectangle(this.height*j, this.width*i, this.height, this.width);
            this.array[j][i] = 0;
            this.field[i][j] = rect;
            rect.setStroke(Color.BLACK);
            rect.setFill(Color.LIGHTGRAY);
            rect.setOnMouseClicked(handler);
            grid.add(rect, j, i);
        }
    } 
    return grid;
}

You could also use 您也可以使用

final int finalI = i;
final int finalJ = j;
rect.setOnMouseClicked(evt -> {
    // TODO: use finalI and finalJ here 
});

in the inner loop instead. 在内部循环中。

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

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