【问题标题】:Javafx How to unclick/disable a button after it's been clickedJavafx如何在单击按钮后取消单击/禁用按钮
【发布时间】: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

    }

【问题讨论】:

标签: java button javafx


【解决方案1】:

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

button.setDisable(true);   

你可以在官方的 Javadoc 中看到 node 是按钮的父级 -> here

【讨论】:

  • 意思是说我对所有这些方法都尝试了true和false,但它仍然不起作用。虽然, setDisable(true) 的奇怪之处在于它使按钮半透明
【解决方案2】:

我不是很擅长编程,但也许我可以在逻辑上提供帮助。

首先,我打算简单地建议在单击按钮时更改按钮的颜色,但我看到您已经这样做了。

如果必须禁用按钮,我会这样做:

点击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),但如果它不在正确的位置,当然它可能不起作用。再说一次,我不知道你在代码的什么地方测试过。

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

最后但同样重要的是,如果你像我一样懒惰,可以查看setOnMouseClicked() 方法。

希望这至少有所帮助。

【讨论】:

  • 好吧,这是一个更简单的解决方案,0% 的帖子都考虑过这个解决方案,我刚刚想出了 setDisabled();是你必须做 setDisable(true);然后设置禁用(假);那么它似乎实际上禁用了按钮,但随后“重新启用它?”我不知道。但我现在可以工作了。我将在原始帖子中发布使其工作的部分代码。
  • 这实际上是我在帖子中所解释的。 :D
  • 嗯,它很相似,但对你来说,它在 2 个不同的 if 语句中具有真假,这意味着它可能并不总是同时使用两者,但它需要背靠背地同时使用。至少你走在正确的轨道上,顺便说一句,它确实有帮助,我会给你的。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 2011-01-20
  • 2017-03-03
相关资源
最近更新 更多