java - 我怎样才能得到为我的程序点击的每个按钮的索引?

标签 java arrays button javafx javafx-8

所以我有一个双排按钮。单击按钮后,我需要能够获取它的索引以用于进一步编码(这是一个类似扫雷游戏的游戏)。到目前为止,这是我的代码。我有一个双 for 循环来为每个按钮创建一个 Handle 事件,但我不知道如何获取每个按钮的索引。我试过 e.getSource() 但它只返回无用的地址。我试过给每个按钮一个 Id 但它只接受字符串。我不知道下一步该怎么做。

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;



public class main extends Application {


Button[][] tiles = new Button[8][8];
Integer[][] mine_field = new Integer[8][8];
Stage window;



public void start(Stage primaryStage){

    BorderPane Field = new BorderPane();
    HBox Start_Show = new HBox(50);
    Button Start = new Button("Start");
    Button Show = new Button ("Show");
    Start.setStyle("-fx-font-size: 15.5pt;");
    Show.setStyle("-fx-font-size: 15.5pt;");
    Start_Show.getChildren().addAll(Start, Show);
    Start_Show.setAlignment(Pos.CENTER);
    Start_Show.setPadding(new Insets(10,10,10,10));
    Field.setTop(Start_Show);



    tiles = create_tiles(tiles);

    int i;
    int j;

    VBox columns = new VBox();

    for(i=0; i<8; i++){
        HBox row = new HBox();
        for(j=0; j<8; j++){
            row.getChildren().add(tiles[i][j]);
        }
        columns.getChildren().add(row);
    }
    columns.setAlignment(Pos.CENTER);
    columns.setPadding(new Insets(0,0,0,100));

    Field.setCenter(columns);



    mine_field = set_mines(mine_field);

    Start.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
             start(primaryStage);               
        }

    });

    Show.setOnAction(new EventHandler<ActionEvent>() {
        @Override
            public void handle(ActionEvent e){

                int i = 0;
                int j = 0;

                for(i=0; i<8; i++){
                    for(j=0; j<8; j++){

                        if(mine_field[i][j] == 1) {
                            tiles[i][j].setStyle("-fx-font-size: 15.5pt;");
                            tiles[i][j].setTextFill(Color.RED);
                            tiles[i][j].setMaxHeight(150);
                            tiles[i][j].setMaxWidth(251);
                            tiles[i][j].setText("M");   
                            }
                        }
                    }
                }
            });



    for(i=0; i<8; i++){

        for(j=0; j<8; j++){
            Tile_Handler tile_handle = new Tile_Handler();
            tiles[i][j].setOnAction(tile_handle);
        }
    }





    Scene scene = new Scene(Field, 600, 600);
    primaryStage.setTitle("MineSweeper!");
    primaryStage.setScene(scene);
    primaryStage.show();


}


public Button[][] create_tiles(Button[][] array){

    int i;
    int j;

    for(i=0; i<8; i++){

        for(j=0; j<8; j++){
            Button my_butt = new Button("?");
            my_butt.setStyle("-fx-font-size: 20pt;");
            my_butt.prefWidthProperty();

            array[i][j] = my_butt;

        }
    }

    return array;

}

public Integer[][] set_mines(Integer[][] array){
    Random random = new Random();

    int i;
    int j;
    int k;

    for(i=0; i<8; i++){

            for(j=0; j<8; j++){
                array[i][j] = 0;

            }
        }

    for(i=0; i<10; i++){
        j = random.nextInt(8);
        k = random.nextInt(8);
        array[j][k] = 1;            
    }

    return array;
}


class Tile_Handler implements EventHandler<ActionEvent> {

    public void handle(ActionEvent e){




        System.out.println("yo clicked one button located at ");
    }
}



public static void main(String[] args){

        Application.launch(args);

}







}

最佳答案

您可以使用实用函数和 lambda 表达式即时创建带有参数的图 block 处理程序:

private EventHandler<ActionEvent> 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));
}

然后像这样应用到不同的按钮:

 ... 

for (int i=0; i<8; i++){
    for (int j=0; j<8; j++) {
        tiles[i][j].setOnAction(createTileHandler(i, j));
    }
}

...

或者,修改您当前的解决方案,您可以向 Tile_Handler 添加两个成员,因此在创建它时将其附加到特定坐标:

class Tile_Handler implements EventHandler<ActionEvent> {

    private int x, y;

    public Tile_Handler(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void handle(ActionEvent e){

        System.out.println(String.format("yo clicked one button located at %d,%d",x,y));
    }
}

然后把实例化改成

Tile_Handler tile_handle = new Tile_Handler(i, j);

关于java - 我怎样才能得到为我的程序点击的每个按钮的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34189259/

相关文章:

java - java中TimerTask调度时出现异常延迟

java - 如何使用java生成包含按升序排列的随机数的数组

javascript - 如何将数组分割成 block ,但让它一个接一个地填充每个数组

kotlin - 我的Kotlin Android Studio应用程序在没有给出值的情况下按按钮时会继续崩溃

JavaScript 事件监听器未添加到按钮

delphi - 在某些组件上更改鼠标光标而不影响其他光标设置代码

Java URLEncode 给出不同的结果

java - 启用ZGC时获取 'allocation stall'

javascript - 如何检查 getElementsByClassName 位置?

java - 无法解析与前缀 'diffgr' 关联的命名空间。在 Castor 中