我对a中的位置计算有疑问 在gridpane中添加的二维按钮数组
班级职位[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);
}
}
跑完后,我得到了这个结果而且不正确
目标是遵循这个逻辑
有什么建议吗?我很感激。
答案 0 :(得分:2)
考虑使用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() );
} );