将文件路径拖放到 Java Swing JTextField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9669530/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:55:39  来源:igfitidea点击:

Drag and Drop file path to Java Swing JTextField

javaswingdrag-and-drop

提问by ewok

Using this question, I created the class below, which handles drag and drop of files to a JTextField. The point of the application is to be able to drag a file into the text field, and have the text field's text set to the file's path (you can see the goal in the code pretty clearly).

使用这个问题,我创建了下面的类,它处理文件到 JTextField 的拖放。应用程序的重点是能够将文件拖入文本字段,并将文本字段的文本设置为文件的路径(您可以很清楚地看到代码中的目标)。

My problem is the below code does not compile. The compilation error states Cannot refer to non-final variable myPanel inside an inner class defined in a different method. I haven't worked much with inner classes, so can seomeone show me how to resolve the error and get the code to behave as designed?

我的问题是以下代码无法编译。编译错误状态Cannot refer to non-final variable myPanel inside an inner class defined in a different method。我对内部类的工作不多,所以有人可以告诉我如何解决错误并使代码按设计运行吗?

Code:

代码:

import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import java.io.File;
import java.util.List;

import javax.swing.*;

public class Test {

public static void main(String[] args) {
    JTextArea myPanel = new JTextArea();

    myPanel.setDropTarget(new DropTarget() {
        public synchronized void drop(DropTargetDropEvent evt) {
            try {
                evt.acceptDrop(DnDConstants.ACTION_COPY);
                List<File> droppedFiles = (List<File>) evt
                        .getTransferable().getTransferData(
                                DataFlavor.javaFileListFlavor);
                for (File file : droppedFiles) {
                    /*
                     * NOTE:
                     *  When I change this to a println,
                     *  it prints the correct path
                     */
                    myPanel.setText(file.getAbsolutePath());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    JFrame frame = new JFrame();
    frame.add(myPanel);
    frame.setVisible(true);

}

}

采纳答案by unholysampler

As the error message says, myPanelneeds to be defined as final.

正如错误消息所说,myPanel需要定义为final。

final JTextArea myPanel = new JTextArea();

This way the inner class can be given one reference pointer to the variable instance without concern that the variable might be changed to point to something else later during execution.

通过这种方式,可以为内部类提供一个指向变量实例的引用指针,而不必担心在执行期间变量可能会更改为指向其他内容。

回答by no one

Another option is to declare the variable static.

另一种选择是将变量声明为静态。

    static JTextArea myPanel = new JTextArea();