- The buttons need to be in a grid like in the second picture
- The buttons need to be square
- The buttons need to resize to fit into the window
Я попробовал привязать ширину pref кнопок с их высотой. Я также пытался изменить ширину ряда сетки. Все это заканчивается неожиданным поведением, когда кнопки не остаются как сетка при изменении размера. Мой код следует.private GridPane drawBlock(Block block) {
GridPane gridPane = new GridPane();
gridPane.getStyleClass().add("board-block");
DoubleProperty doubleProperty = new SimpleDoubleProperty();
for (int i = 0; i < block.getRowsList().size(); i++) {
for (int j = 0; j < block.getRowsList().get(i).getBoxes().size(); j++) {
StateButton stateButton = new StateButton();
stateButton.setBox(block.getRowsList().get(i).getBoxes().get(j));
stateButton.setOnAction(e -> stateButton.updateState());
stateButton.textProperty().bind(stateButton.getBox().stateIconProperty());
// stateButton.maxHeightProperty().bind(stateButton.widthProperty());
stateButton.prefHeightProperty().bind(stateButton.widthProperty());
// stateButton.minHeightProperty().bind(stateButton.widthProperty());
stateButton.getBox().setState(Box.BoxState.UNSURE);
stateButton.getBox().update();
if (i+j==0) doubleProperty.bind(stateButton.widthProperty());
}
}
RowConstraints rowConstraints = new RowConstraints();
rowConstraints.prefHeightProperty().bind(doubleProperty);
gridPane.getRowConstraints().add(rowConstraints);
return gridPane;
}
< /code>
Catebutton Code < /p>
@Setter
@Getter
public class StateButton extends Button {
private Box box;
public void updateState() {
box.update();
}
public StateButton() {
super();
getStyleClass().add("state-button");
}
}
< /code>
Код класса блока < /p>
@AllArgsConstructor
public class Box {
@Getter
private BoxState state;
private StringProperty stateIcon;
private List eliminationListeners = new ArrayList();
public Box() {
state = BoxState.UNMARKED;
stateIcon = new SimpleStringProperty(state.getIcon());
}
public Box(BoxState newState) {
this.state = newState;
this.stateIcon = new SimpleStringProperty(newState.getIcon());
}
public void setState(BoxState state){
this.state = state;
stateIcon.setValue(state.getIcon());
}
public void addEliminationListener(EliminationListener listener) {
eliminationListeners.add(listener);
}
public StringProperty stateIconProperty() { return stateIcon; }
public final String getSateIcon() { return stateIcon.getValue(); }
public final void setStateIcon(String icon) {
stateIcon.setValue(icon);
}
public void populateFromBox(Box box) {
this.setState(box.getState());
}
public Box clone() {
return new Box(state);
}
public void update() {
state = state.update(eliminationListeners);
switch (state) {
case TRUE -> eliminationListeners.forEach(EliminationListener::eliminate);
case UNSURE -> eliminationListeners.forEach(EliminationListener::uneliminate);
}
setStateIcon(state.getIcon());
}
public void eliminate() {
state = state.onEliminated();
stateIcon.setValue(state.getIcon());
}
public void uneliminate() {
state = state.onUneliminated(eliminationListeners);
stateIcon.setValue(state.getIcon());
}
public enum BoxState {
UNMARKED{
@Override
public BoxState update(List eliminationListeners) {
return BoxState.FALSE;
}
@Override
public BoxState onEliminated() {
return BoxState.FALSE_BY_ELIMINATION;
}
@Override
public String getIcon() { return ""; }
},
FALSE {
@Override
public BoxState update(List eliminationListeners) {
return checkEliminable(eliminationListeners) ? BoxState.FALSE : BoxState.TRUE;
}
@Override
public String getIcon() { return "
},
TRUE {
@Override
public BoxState update(List eliminationListeners) {
return BoxState.UNSURE;
}
@Override
public String getIcon() { return "
},
UNSURE {
@Override
public BoxState update(List eliminationListeners) {
return checkEliminable(eliminationListeners) ? BoxState.FALSE_BY_ELIMINATION : BoxState.UNMARKED; //good
}
@Override
public String getIcon() { return "
},
FALSE_BY_ELIMINATION {
@Override
public BoxState onUneliminated(List eliminationListeners) {
return !checkEliminable(eliminationListeners) ? BoxState.UNMARKED : BoxState.FALSE_BY_ELIMINATION;
}
@Override
public String getIcon() { return "ⅹ"; }
};
public BoxState update(List eliminationListeners) {
return this;
}
public BoxState onEliminated() {
return this;
}
public BoxState onUneliminated(List eliminationListeners) {
return this;
}
public abstract String getIcon();
private static boolean checkEliminable(List eliminationListeners) {
return eliminationListeners.stream().anyMatch(EliminationListener::checkEliminable);
}
}
}
< /code>
block class < /p>
@Data
@AllArgsConstructor
public class Block {
private List rowsList;
private List columnsList;
private SuspectType rowType;
private SuspectType columnType;
public Block(List
Подробнее здесь: https://stackoverflow.com/questions/797 ... each-other