gpt4 book ai didi

java - 获取添加到网格 Pane 的按钮数组中位置 [x] [y] 的值

转载 作者:行者123 更新时间:2023-11-30 03:21:59 25 4
gpt4 key购买 nike

我对a中头寸的计算有疑问添加到网格 Pane 中的二维按钮数组

类(class)位置[lin,col]

public class Position {

private int lin;
private char col;

public Position(int lin, char col) {
this.col = col;
this.lin = lin;
}

@Override
public String toString() {
return col + "" + lin;
}
}

类 GridPaneArrayButton

public class GridPaneArrayButton extends Application {

@Override
public void start(Stage primaryStage) {

Image[][] img1 = new Image[9][9];
Image[][] img2 = new Image[9][9];

GridPane root = new GridPane();
root.setPadding(new Insets(5));
root.setAlignment(Pos.CENTER);

Label label = new Label("CHESS TABLE 8X8");
label.setFont(Font.font("Cambria", FontWeight.BOLD, 18));
GridPane.setColumnSpan(label, 10);
GridPane.setRowSpan(label, 2);
root.add(label, 0, 0);
GridPane.setHalignment(label, HPos.CENTER);

TextArea text = new TextArea();
GridPane.setColumnSpan(text, 10);
GridPane.setRowSpan(text, 2);
root.setVgap(1);
root.add(text, 0, 25);
GridPane.setHalignment(text, HPos.CENTER);
text.getStyleClass().add("text-area");

Button[][] btn = new Button[8][8];

boolean changeColor = false;

for (int i = 0; i < btn.length; i++) {

for (int j = 0; j < btn.length; j++) {


btn[i][j] = new Button("", new ImageView(img1[i][j]));

btn[1][j] = new Button("", new ImageView(img1[0][8]));

btn[6][j] = new Button("", new ImageView(img2[7][8]));

btn[7][j] = new Button("", new ImageView(img2[i][j]));

if (changeColor == false) {
btn[i][j].getStyleClass().add("btn_color_lightgrey");
} else {
btn[i][j].getStyleClass().add("btn_color_grey");
}
btn[i][j].setPrefSize(65, 65);
root.add(btn[i][j], j, i + 2);
btn[i][j].setOnMouseClicked((MouseEvent e) -> {

StringBuilder str = new StringBuilder();

str.append("Position Gridpane X: ").append(e.getX()).append("\n");
str.append("Position Gridpane Y: ").append(e.getY()).append("\n");

int x = (int) e.getX();
int y = (int) e.getY();

int rx = ((int) e.getX() % 8);
int ry = ((int) e.getY() % 8);

int lin = (x - rx) / 8;
int col = (y - ry) / 8;

str.append("Line X: ").append(lin).append("\n");
str.append("Col Y: ").append(convCol(col)).append("\n");
str.append("Position XY : ").append(new Position(lin, convCol(col))).append("\n");
str.append("\n-----------------------------------------------------------------------------\n\n");

text.appendText(str.toString());
});
changeColor = !changeColor;
}
changeColor = !changeColor;
}

Scene scene = new Scene(root, 900, 900);
scene.getStylesheets().add(this.getClass().getResource("styles.css").toExternalForm());

primaryStage.setTitle("Grid Pane Example");
primaryStage.setScene(scene);
primaryStage.show();
}

public static char convCol(int col) {
switch (col) {
case 1:
return 'A';
case 2:
return 'B';
case 3:
return 'C';
case 4:
return 'D';
case 5:
return 'E';
case 6:
return 'F';
case 7:
return 'G';
case 8:
return 'H';
default:
return ' ';
}
}

public static void main(String[] args) {
launch(args);
}
}

运行后,我得到这个结果,这是不正确的

enter image description here

目标是遵循这个逻辑

enter image description here

有什么建议吗?我将不胜感激。

最佳答案

考虑使用Node.setUserData()将按钮的位置存储在按钮本身中,并消除根据鼠标单击坐标来确定它:

// store the position
btn[i][j].setUserData( new Position( btn.length - i, convCol( j + 1 ) ) );

// get it on mouse click
btn[i][j].setOnMouseClicked( ( MouseEvent e ) -> {
StringBuilder str = new StringBuilder();
str.append( "Position XY : " ).append( (( Node ) e.getSource()).getUserData() ).append( "\n" );
text.appendText( str.toString() );
} );

关于java - 获取添加到网格 Pane 的按钮数组中位置 [x] [y] 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31128567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com