java JavaFx 将文件拖放到程序中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32534113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFx Drag and Drop a file INTO a program
提问by TheHoop
Hey there community I was wondering if is possible to create a program that allows for the user to Drag a file from anywhere on there hard drive (the desktop, documents folder, videos folder) and drop it into the window of the program.
嘿那里的社区我想知道是否可以创建一个程序,允许用户从硬盘驱动器上的任何位置(桌面、文档文件夹、视频文件夹)拖动文件并将其放入程序窗口。
I am creating a media player and I want to be able to play a video by dragging and dropping a MP4 into the window. Do I need to store the file in a variable, or just the location of the file into a variable. Also, it is important I keep support for cross platform.
我正在创建一个媒体播放器,我希望能够通过将 MP4 拖放到窗口中来播放视频。我是否需要将文件存储在变量中,或者只是将文件的位置存储到变量中。此外,保持对跨平台的支持也很重要。
I am using JavaFx with java 7 update 79 jdk.
我正在将 JavaFx 与 java 7 update 79 jdk 一起使用。
Thanks in advance.
提前致谢。
回答by WillShackleford
Here is a simple drag and drop example that just sets the file name and location. Drag files to it and it shows their name and location. Once you know that it should be a completely separate matter to actually play the file. It is primarily taken from Oracle's documentation: https://docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm
这是一个简单的拖放示例,它只设置文件名和位置。将文件拖到它上面,它会显示它们的名称和位置。一旦您知道实际播放文件应该是完全独立的事情。它主要取自 Oracle 的文档:https: //docs.oracle.com/javafx/2/drag_drop/jfxpub-drag_drop.htm
A minimal implementation needs two EventHandler
s set OnDragOver and OnDragDropped.
一个最小的实现需要两个EventHandler
设置 OnDragOver 和 OnDragDropped。
public class DragAndDropTest extends Application {
@Override
public void start(Stage primaryStage) {
Label label = new Label("Drag a file to me.");
Label dropped = new Label("");
VBox dragTarget = new VBox();
dragTarget.getChildren().addAll(label,dropped);
dragTarget.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
if (event.getGestureSource() != dragTarget
&& event.getDragboard().hasFiles()) {
/* allow for both copying and moving, whatever user chooses */
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
event.consume();
}
});
dragTarget.setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasFiles()) {
dropped.setText(db.getFiles().toString());
success = true;
}
/* let the source know whether the string was successfully
* transferred and used */
event.setDropCompleted(success);
event.consume();
}
});
StackPane root = new StackPane();
root.getChildren().add(dragTarget);
Scene scene = new Scene(root, 500, 250);
primaryStage.setTitle("Drag Test");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
回答by Moritz Schmidt
When working with Drag and Drop events, you could try the following:
使用拖放事件时,您可以尝试以下操作:
Obtain a Dragboard
-object of the DragEvent
and work with the method getFiles
:
获取Dragboard
的对象DragEvent
并使用该方法getFiles
:
private void handleDragDropped(DragEvent event){
Dragboard db = event.getDragboard();
File file = db.getFiles().get(0);
}