我已经在我的游戏中实现了拖放功能,但到目前为止,我只能“拖放”到硬编码的位置。如图所示:
我想要的是:
我的setOnDragDropped事件在此处处理:
//Drag dropped draws the image to the receiving node
target.setOnDragDropped(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
//Data dropped
//If there is an image on the dragboard, read it and use it
Dragboard db = event.getDragboard();
boolean success = false;
int x, y;
if(db.hasImage()){
//target.setText(db.getImage()); --- must be changed to target.add(source, col, row)
//target.add(source, 5, 5, 1, 1);
//Places at 0,0 - will need to take coordinates once that is implemented
Board.add(test, 0, 0, 1, 1);
success = true;
}
//let the source know whether the image was successfully transferred and used
event.setDropCompleted(success);
event.consume();
}
});
我觉得这应该是一个简单的鼠标悬停事件或类似的事情,但我不知道该怎么做。
编辑:下面类的完整代码:
public class Controller implements Initializable{
@FXML
public GridPane Board;
public GridPane ShipsToBePlaced;
public Rectangle MessageBox;
public Button ReadyButton;
public Button QuitButton;
public ImageView[][] water;
public ImageView[] ships;
public ImageView[][] ships2d;
@Override
public void initialize(URL location, ResourceBundle resources) {
//Adds water to each cell in grid
water = new ImageView[10][10];
//ships2d = new ImageView[10][10];
for(int i =0; i<10; i++){
for(int j=0; j <10; j++){
water[i][j] = new ImageView("Tiles/watertile.png");
water[i][j].setPreserveRatio(true);
water[i][j].setFitHeight(49);
water[i][j].setFitWidth(49);
Board.add(water[i][j], i, j);
//ships2d[i][j] = new ImageView("Ships/ship2.png");
//ships2d[i][j].setPreserveRatio(true);
//ships2d[i][j].setFitWidth(49);
//Board.add(ships2d[i][j], i, j);
}
}
//Adds ships
ships = new ImageView[5];
ships[0] = new ImageView("Ships/ship2.png");
ships[1] = new ImageView("Ships/ship3.png");
ships[2] = new ImageView("Ships/ship3.png");
ships[3] = new ImageView("Ships/ship4.png");
ships[4] = new ImageView("Ships/ship5.png");
for(int i=0; i < 5; i++){
ships[i].setPreserveRatio(true);
}
ships[0].setFitWidth(80);
ships[1].setFitWidth(120);
ships[2].setFitWidth(120);
ships[3].setFitWidth(160);
ships[4].setFitWidth(200);
//ShipsToBePlaced.add(ships[0], 0, 0);
ShipsToBePlaced.add(ships[1], 0, 1);
ShipsToBePlaced.add(ships[2], 0, 2);
ShipsToBePlaced.add(ships[3], 0, 3);
ShipsToBePlaced.add(ships[4], 0, 4);
//Test imageview for dropped ship
ImageView test = new ImageView("Ships/ship2.png");
test.setPreserveRatio(true);
test.setFitWidth(80);
//First attempt at drag and drop
ImageView source = new ImageView ("Ships/ship2.png");
source.setPreserveRatio(true);
source.setFitWidth(80);
ShipsToBePlaced.add(source, 0, 0);
final GridPane target = Board;
//Drag detected event handler is used for adding drag functionality to the boat node
source.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
//Drag was detected, start drap-and-drop gesture
//Allow any transfer node
Dragboard db = source.startDragAndDrop(TransferMode.ANY);
//Put ImageView on dragboard
ClipboardContent cbContent = new ClipboardContent();
cbContent.putImage(source.getImage());
//cbContent.put(DataFormat.)
db.setContent(cbContent);
source.setVisible(false);
event.consume();
}
});
//Drag over event handler is used for the receiving node to allow movement
target.setOnDragOver(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
//data is dragged over to target
//accept it only if it is not dragged from the same node
//and if it has image data
if(event.getGestureSource() != target && event.getDragboard().hasImage()){
//allow for moving
event.acceptTransferModes(TransferMode.MOVE);
}
event.consume();
}
});
//Drag entered changes the appearance of the receiving node to indicate to the player that they can place there
target.setOnDragEntered(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
//The drag-and-drop gesture entered the target
//show the user that it is an actual gesture target
if(event.getGestureSource() != target && event.getDragboard().hasImage()){
source.setVisible(false);
target.setOpacity(0.7);
System.out.println("Drag entered");
}
event.consume();
}
});
//Drag exited reverts the appearance of the receiving node when the mouse is outside of the node
target.setOnDragExited(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
//mouse moved away, remove graphical cues
source.setVisible(true);
target.setOpacity(1);
event.consume();
}
});
//Drag dropped draws the image to the receiving node
target.setOnDragDropped(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
//Data dropped
//If there is an image on the dragboard, read it and use it
Dragboard db = event.getDragboard();
boolean success = false;
int x, y;
if(db.hasImage()){
//target.setText(db.getImage()); --- must be changed to target.add(source, col, row)
//target.add(source, 5, 5, 1, 1);
//Places at 0,0 - will need to take coordinates once that is implemented
Board.add(test, 0, 0, 1, 1);
success = true;
}
//let the source know whether the image was successfully transferred and used
event.setDropCompleted(success);
event.consume();
}
});
source.setOnDragDone(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
//the drag and drop gesture has ended
//if the data was successfully moved, clear it
if(event.getTransferMode() == TransferMode.MOVE){
source.setVisible(false);
}
event.consume();
}
});
}
}
如果您可以访问实际删除该节点的节点,则可以在网格窗格中确定索引,并为新节点使用相同的索引和/或以任何其他方式使用索引。。。
在这种情况下,您可以使用DragEvent.getPickResult(). getIntersectedNode()
或Event.getTarget
获取目标ImageView
:
public void handle(DragEvent event) {
//Data dropped
//If there is an image on the dragboard, read it and use it
Dragboard db = event.getDragboard();
boolean success = false;
Node node = event.getPickResult().getIntersectedNode();
if(node != target && db.hasImage()){
Integer cIndex = GridPane.getColumnIndex(node);
Integer rIndex = GridPane.getRowIndex(node);
int x = cIndex == null ? 0 : cIndex;
int y = rIndex == null ? 0 : rIndex;
//target.setText(db.getImage()); --- must be changed to target.add(source, col, row)
//target.add(source, 5, 5, 1, 1);
//Places at 0,0 - will need to take coordinates once that is implemented
ImageView image = new ImageView(db.getImage());
// TODO: set image size; use correct column/row span
Board.add(image, x, y, 1, 1);
success = true;
}
//let the source know whether the image was successfully transferred and used
event.setDropCompleted(success);
event.consume();
}
注意:由于您使用系统剪贴板使用拖动事件,因此其他应用程序可能是拖动手势的源或目标...
我正在尝试将自定义对象从JPanel拖放到JavaFX场景<为了实现这一点,我创建了一个带有自定义对象和自定义TransferHandler的简单应用程序。在本例中,我的问题是,当我在
我喜欢用Javafx和FXML布局编写一个九人莫里斯游戏,我将使用场景生成器进行图形部分。 谁能给我写一个关于拖放的小例子吗?因为这是一场九人莫里斯的比赛,我只喜欢拖拽,并有一些我可以扔下碎片的点。
我正在做的是为一副牌中的每张牌创建一个图像视图。对于每个图像视图,我添加了两个事件setOnDragOver和setondragdroped。然而,当我点击并试图拖动一张卡片时,我的活动打印声明甚至没有显示。 我试图做的是允许在窗格上拖动一张卡。因此,根据我拖动它的位置更改位置,但它仍然在窗口上。下面是图像视图的事件。 这个是为了脱线
我有一个ImageView,可以用鼠标拖放,点击按钮旋转90度。这两件事在独立完成时都可以工作(因此可以旋转或移动ImageView,而不是两者都可以)。当我旋转ImageView并尝试移动它时,它似乎是随机移动的。 我使用以下方法旋转图像视图: 这导致了我所说的看似随机的运动。 我还尝试使用以下方法旋转ImageView: 位置是图像在屏幕上的位置。宽度和高度是ImageView的宽度和高度。
我正在创建一个带有拖放特性的javafx应用程序。我需要能够从一个窗格拖拽到下一个窗格。丢弃后,所有被丢弃的节点都需要是可访问的。拖放操作很顺利。然而,我有一个问题,其中不是所有的节点都可以访问后删除。我会拖动一个节点并点击它,有时它会做出响应,但有时当我点击它时,一个随机节点将不再显示功能。对于哪些节点停止工作,哪些节点始终工作,没有明显的模式(我可以看到)。如果任何人有任何关于为什么这可能是一
我正在尝试一个简单的Java FX程序,它可以创建一个窗口,我所需要的只是增加读取已删除文件的能力。我得到了以下代码片段,它不会抛出任何错误,但也不允许我删除任何内容。当我试图删除文件时,它会显示一个红色光标(不允许使用windows光标) 我使用的是Windows 8机器和JDK 8.0_60版。不确定问题出在哪里。代码有问题吗?我错过什么了吗? 我试过的- 已尝试更改传输模式 尝试检查文件权限