你需要實現「全壓阻力釋放手勢」,如MouseEvent
documentation描述。
這裏是一個快速演示(儘管標籤'狀態的更新不是非常有效,你可以通過一些工作來優化它。
import javafx.application.Application;
import javafx.css.PseudoClass;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class DragToSelect extends Application {
private class Location { int x; int y ;}
private PseudoClass selectedClass = PseudoClass.getPseudoClass("selected");
@Override
public void start(Stage primaryStage) {
Label[][] matrix = new Label[40][40];
boolean[][] selected = new boolean[40][40];
GridPane gridPaneMatrix = new GridPane();
Location dragAnchor = new Location();
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++) {
Location loc = new Location();
loc.x = i ;
loc.y = j ;
matrix[i][j] = new Label();
matrix[i][j].setAccessibleHelp(i+","+j);
matrix[i][j].getStyleClass().add("classic-label");
matrix[i][j].setOnMousePressed(event -> {
dragAnchor.x = loc.x ;
dragAnchor.y = loc.y ;
selected[loc.x][loc.y] = ! selected[loc.x][loc.y];
matrix[loc.x][loc.y].pseudoClassStateChanged(selectedClass, selected[loc.x][loc.y]);
});
matrix[i][j].setOnDragDetected(e -> {
dragAnchor.x = loc.x ;
dragAnchor.y = loc.y ;
updateState(selected, matrix, dragAnchor, loc);
matrix[loc.x][loc.y].startFullDrag();
});
matrix[i][j].setOnMouseDragEntered(e -> {
System.out.printf("Dragged [%d, %d]%n", loc.x, loc.y);
updateState(selected, matrix, dragAnchor, loc);
});
gridPaneMatrix.add(matrix[i][j], i, j);
}
Scene scene = new Scene(gridPaneMatrix);
scene.getStylesheets().add("style.css");
primaryStage.setScene(scene);
primaryStage.show();
}
private void updateState(boolean[][] selected, Label[][] matrix, Location anchor, Location target) {
for (int x = 0 ; x < selected.length ; x++) {
for (int y = 0 ; y < selected[x].length; y++) {
selected[x][y] = x >= Math.min(anchor.x, target.x)
&& x <= Math.max(anchor.x, target.x)
&& y >= Math.min(anchor.y, target.y)
&& y <= Math.max(anchor.y, target.y) ;
matrix[x][y].pseudoClassStateChanged(selectedClass, selected[x][y]);
}
}
}
public static void main(String[] args) {
launch(args);
}
}
的style.css:
.classic-label {
-fx-pref-width: 10 ;
-fx-pref-height: 15 ;
-fx-background: black ;
-fx-background-color: orange, -fx-background ;
-fx-background-insets: 0, 1 1 0 0 ;
}
.classic-label:selected {
-fx-background: limegreen ;
}