【发布时间】:2021-04-24 01:00:55
【问题描述】:
我正在尝试构建一个看起来像这样的nonogram
但目前我的代码会像这样显示线索(顶部和左侧的数字)
当我希望将它们分隔成行时,这些线索会堆叠在一起。
这是我目前使用 JavaFX 的代码:
public class PuzzleView implements FXComponent {
private Controller controller;
private Pane board;
private Pane entirePuzzle;
private VBox[] vertClues;
private HBox[] horzClues;
private Pane[][] tiles;
public PuzzleView(Controller controller) {
this.controller = controller;
}
@Override
public Parent render() {
entirePuzzle = new Pane(); // grid pane
initBoard();
initLabels();
entirePuzzle.getChildren().add(board);
return entirePuzzle;
}
private void initBoard() {
board = new Pane();
int width = controller.getClues().getWidth();
int height = controller.getClues().getHeight();
board.setLayoutX(100);
board.setLayoutY(100);
for (int i=0; i<2; i++) {
for (int j=0; j<2; j++) {
Rectangle clues = new Rectangle();
clues.setWidth(width);
clues.setHeight(height);
clues.setLayoutX(i*(width*50));
clues.setLayoutY(j*height*50);
clues.setStroke(Color.LIGHTBLUE);
clues.setStrokeWidth(2);
clues.setFill(Color.WHITE);
board.getChildren().add(clues);
}
}
tiles = new Pane[width][height];
for (int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
tiles[i][j] = new Pane();
Rectangle rect = new Rectangle(50, 50);
rect.setStroke(Color.LIGHTGRAY);
rect.setStrokeWidth(1);
rect.setFill(Color.TRANSPARENT);
tiles[i][j].getChildren().add(rect);
tiles[i][j].setLayoutX(i*50);
tiles[i][j].setLayoutY(j*50);
board.getChildren().add(tiles[i][j]);
}
}
}
private void initLabels() {
int width = controller.getClues().getWidth();
int height = controller.getClues().getHeight();
vertClues = new VBox[height];
horzClues = new HBox[width];
Clues clue = controller.getClues();
for (int i = 0; i < width; i++) {
horzClues[i] = new HBox();
horzClues[i].setLayoutX(100 + i * width);
for (int j=0; j<controller.getClues().getColCluesLength(); j++) {
horzClues[i].setSpacing(width);
Label label = new Label(String.valueOf(clue.getColClues(i)[j]));
label.minHeight(25);
label.minWidth(25);
horzClues[i].getChildren().add(label);
}
entirePuzzle.getChildren().add(horzClues[i]);
}
for (int i = 0; i < height; i++) {
vertClues[i] = new VBox();
vertClues[i].setLayoutY(100 + i * height);
for (int j=0; j<controller.getClues().getRowCluesLength(); j++) {
Label label = new Label(String.valueOf(clue.getRowClues(i)[j]));
label.minHeight(25);
label.minWidth(25);
vertClues[i].getChildren().add(label);
}
entirePuzzle.getChildren().add(vertClues[i]);
}
}
}
FXComponent 是我创建的一个接口,它只有一个 render() 方法,它返回一个组件对象,我可以将它放在不同的类中。 controller 是我创建的另一个类,它允许我访问拼图本身的属性和其他功能。
【问题讨论】:
-
您应该在此布局中使用
GridPane,不是吗?通过尝试自己手动构建布局,您所做的工作比您需要的要多得多。 -
当我试图将
entirePuzzle更改为 GridPane 时,一切都变得更糟,而且到处都是。我尝试阅读有关如何定位东西的文档,但无法将其缠绕在我的头上,它们都只是……有点卡在那里。 -
我认为与其重新发布您删除的other question,不如简单地编辑原始问题。