Javafx如何在单击按钮后取消单击/禁用按钮

时间:2019-08-13 18:18:44

标签: java button javafx

编辑:底部的解决方案

这是一款跳棋游戏。单击一个按钮后,它等待另一个按钮被单击以与之交换。但是,有时您可能不想移动该按钮,但是一旦单击它,就无法回头,因为我无法禁用它。

从这里的其他帖子中,我已经看到人们在使用

button.setVisable(false);    

这只是使它在第一次单击后不可见。

button.setCancelButton(false);    

这什么也没做。

button.setDisable(false);    

这也不做任何事情。 编辑:并且所有这些方法都尝试过true和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;



}

private void swap(){

    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 collor back to normal

                clickCounter = 0;
                return;                   //reset counter and return to top of buttonClicked() method

    }

2 个答案:

答案 0 :(得分:1)

要禁用按钮,需要将disabled-property设置为true。

button.setDisable(true);   

您可以在节点的官方Javadoc中看到它是按钮的父级-> here

答案 1 :(得分:1)

我不是很擅长编程,但是也许我可以帮助您解决逻辑问题。

首先,我只是建议您简单地单击按钮时更改按钮的颜色,但是我发现您已经这样做了。

如果必须禁用该按钮,这就是我要做的:

点击button1 =>

button1.setStyle("-fx-background-color: LightGreen"); // Change color

button1.setDisabled(true); // Disables the button - cannot be clicked

“哦,我改变了主意!我想使用另一个按钮。” =>

GridPane =>

上单击任意位置

显示System.out.println("Can't move diagonally");System.out.println("Can't move more than one spot"); =>

button1.setDisabled(false); // Enables the button - can be clicked


基本上,在您的代码中,将这些行添加到这些位置:

    ............
    Button button = (Button) source;

    if (clickCounter == 0) {

        first = button;

        first.setStyle("-fx-background-color: LightGreen");

        first.setDisabled(true);  // This line

    } else {

        ............
    }
    ............
    ............
    if (firstCol != secondCol) {

        clickCounter = 0;

        System.out.println("Can't move diagonally");

        first.setDisabled(false); // This line

        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");

        first.setDisabled(false); // This line

        return false;
    }
    ............

您可能已经尝试过setDisabled(true/false),但是,如果未将其放在正确的位置,则可能无法使用。再说一次,我不知道您在代码中的哪个位置进行了测试。

如果您无法使用first方法中的legalMove()按钮,只需将其作为参数传递即可。

最后但并非最不重要的一点是,如果您像我一样懒惰,可以签出setOnMouseClicked()方法。

希望这至少可以有所帮助。

相关问题