Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击时Java FX接收形状数组的索引_Java_Javafx_Shape_Mouseclick Event - Fatal编程技术网

单击时Java FX接收形状数组的索引

单击时Java FX接收形状数组的索引,java,javafx,shape,mouseclick-event,Java,Javafx,Shape,Mouseclick Event,我正在尝试构建一个战舰应用程序。到目前为止,我成功地为玩家和敌人显示了两个区域。一个字段由10 x 10个矩形组成-一个VBox中有10个HBox。这是创建字段的方法: private Rectangle[][] field; public VBox CreateField(){ VBox vbox = new VBox(2); for(int i = 0; i < this.columns; ++i){ HBox hbox

我正在尝试构建一个战舰应用程序。到目前为止,我成功地为玩家和敌人显示了两个区域。一个字段由10 x 10个矩形组成-一个VBox中有10个HBox。这是创建字段的方法:

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;
    }
私有矩形[][]字段;
公共VBox CreateField(){
VBox VBox=新的VBox(2);
对于(int i=0;i
结果是:

当用户单击某个矩形时,我需要接收该矩形的索引


你们能给我一个例子/代码剪辑如何完成我的问题吗?

Lol,得到它的速度比我想象的要快。这是我的解决方案()

当我单击一个形状时。
为这些问题道歉。

哈哈,我的工作比我想象的要快。这是我的解决方案()

当我单击一个形状时。
对此问题深表歉意。

我建议使用
GridPane
而不是
HBox
es和
VBox
。您可以将循环索引复制到
final
局部变量,以从循环体内部创建的匿名
EventHandler
class/lambda表达式访问它们,或者可以在
GridPane
子项上使用
GridPane.getRowIndex
GridPane.getColumnIndex

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;
}

我建议使用
GridPane
而不是
HBox
es和
VBox
。您可以将循环索引复制到
final
局部变量,以从循环体内部创建的匿名
EventHandler
class/lambda表达式访问它们,或者可以在
GridPane
子项上使用
GridPane.getRowIndex
GridPane.getColumnIndex:

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;
}
而是在内部循环中

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