![](/img/trans.png)
[英]How to disable setOnMouseClicked while scrolling in JavaFX ListView
[英]Is there any other way to implement setOnMouseClicked on JavaFX
我必須在 javaFX 中創建一個矩陣。 我用 GridPane 創建它時沒有任何問題。 接下來是在矩陣右側創建類似“按鈕”,這些按鈕會將矩陣的 +1 元素向右移動。 像這樣:
110 <-
101 <- //ie: I clicked this button
100 <-
The result:
110 <-
110 <-
100 <-
我處理這種位移移動的方式是使用循環鏈表。 我對此沒有任何問題,我認為您可以省略那部分。 我使用這種方法:
private void moveRowRight(int index){
//recives a index row and moves +1 the elements of that row in the matrix.
}
cells
是矩陣
問題是,首先矩陣可以通過用戶輸入來修改,即5x5 6x6 7x7,所以按鈕的數量也會改變。 我嘗試使用 BorderPane(center: gridpane( matrix ), right: VBox()) ,這是我如何在 Vbox (邊框窗格的右側)內添加一些 HBox 並使用setOnMouseClicked
的代碼的一部分。
private void loadButtonsRight(){
for(int i = 0; i < cells[0].length ; i++){
HBox newBox = new HBox();
newBox.getChildren().add(new Text("MOVE"));
newBox.prefHeight(50);
newBox.prefWidth(50);
newBox.setOnMouseClicked(e -> {
moveRowRight(i);
});
VBRightButtons.getChildren().add(newBox); //where I add the HBox to the VBox (right part of the Border Pane)
}
}
}
但是接下來就出現了這個問題。
Local variables referenced from lambda expression must be final or effectively final
似乎我無法實現 lambda 的值會發生變化。 有什么方法可以幫助我放置取決於矩陣大小並使用我創建的方法的“按鈕”?
只需聲明一個最終值並使用它。
final int idx = i;
newBox.setOnMouseClicked(e ->
moveRowRight(idx);
);
如果您想進一步了解這一點,請參閱 baeldung 教程
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.