【发布时间】:2019-08-13 18:18:44
【问题描述】:
编辑:底部的解决方案
这是一个跳棋游戏。单击一个按钮后,它会等待单击第二个按钮与之交换。但是,有时您可能不想移动该按钮,但是一旦单击它,就无法回头,因为我无法禁用它。
从这里的其他帖子中,我看到人们使用
button.setVisable(false);
这只是使它在第一次点击后不可见。
button.setCancelButton(false);
这没有任何作用。
button.setDisable(false);
这也没有任何作用。 编辑:所有这些方法都尝试过真假。
private void buttonClicked(Object source) {
boolean bool = false;
if (!(source instanceof Button)) {
return;
}
Button button = (Button) source;
if (clickCounter == 0) {
first = button;
first.setStyle("-fx-background-color: LightGreen");
} else {
second = button;
bool = legalMove(GridPane.getRowIndex(first), GridPane.getColumnIndex(first), GridPane.getRowIndex(second), GridPane.getColumnIndex(second), source);
//swap();
System.out.println("checking the bool " + bool);
}
if(bool == true){
swap();
}
else{
System.out.println("Button disabled");
//first.setVisible(false);
//first.setCancelButton(true);
//first.setDisable(false);
clickCounter = 0;
}
System.out.println(clickCounter + " " + ((Button) source).getText());
clickCounter = ++clickCounter % 2;
}
私人无效交换(){
int firstRow = GridPane.getRowIndex(first);
int firstCol = GridPane.getColumnIndex(first);
int secondRow = GridPane.getRowIndex(second);
int secondCol = GridPane.getColumnIndex(second);
grid.getChildren().removeAll(first, second);
grid.add(first, secondCol, secondRow);
second.setText(" ");
grid.add(second, firstCol, firstRow);
first.setStyle(null);
}
private boolean legalMove(int firstRow, int firstCol, int secondRow, int secondCol, Object source) {
System.out.println("returned back");
//can only move up/down, not diagonally
if (firstCol != secondCol) {
clickCounter = 0;
System.out.println("Can't move diagonally");
return false;
}
//test if the move is no more than 1 spot away
if ((secondRow - firstRow) != 1 && (firstRow - secondRow) != 1) {
clickCounter = 0;
System.out.println("Can't move more than one spot");
return false;
}
return killCheck();
}
单击按钮后,您应该能够再次单击它,或者单击错误的动作,或者甚至我可能会在侧面有一个按钮,允许用户禁用按钮,以便他们可以选择使用不同的按钮。
编辑:解决方案
所以要真正禁用你必须使用的按钮
setDisable(true);
但是你也需要
setDisable(false);
这样它会重新启用它,或者其他什么,我实际上不知道为什么会这样。
if(bool == false && clickCounter == 1){
System.out.println("Button disabled");
first.setDisable(true);
first.setDisable(false);
first.setStyle(null); //this sets the button color back to normal
clickCounter = 0;
return; //reset counter and return to top of buttonClicked() method
}
【问题讨论】:
-
请提供一个minimal reproducible example 来说明问题。