簡體   English   中英

Javafx 如何在單擊后取消單擊/禁用按鈕

[英]Javafx How to unclick/disable a button after it's been clicked

編輯:底部的解決方案

這是跳棋游戲。 單擊一個按鈕后,它會等待單擊第二個按鈕與它交換。 但是,有時您可能不想移動該按鈕,但是一旦您單擊它,就無法回頭,因為我無法禁用它。

從這里的其他帖子中,我看到人們使用

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;



}

私人無效交換(){

    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

    }

要禁用按鈕,您需要將 disabled-property 設置為 true。

button.setDisable(true);   

您可以在作為按鈕父級的節點的官方 Javadoc 中看到這一點 -> 此處

我不太擅長編程,但也許我可以幫助解決邏輯問題。

首先,我只是建議在單擊按鈕時更改按鈕的顏色,但我看到您已經這樣做了。

如果必須禁用按鈕,這就是我要做的:

單擊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()方法。

希望這至少有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM