java使用JFileChooser访问选定文件
有段时间没编码了,所以我觉得我有点生疏了。我正在尝试构建一个应用程序,让用户选择一个文件作为输入。下面是我目前拥有的代码:
JButton btnFile = new JButton("Select Excel File");
btnFile.addActionListener(new ActionListener() {
//Handle open button action.
public void actionPerformed(ActionEvent e) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(frmRenamePdfs);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
System.out.println("File: " + file.getName() + ".");
} else {
System.out.println("Open command cancelled by user.");
}
System.out.println(returnVal);
}
});
我似乎不知道如何从监听器外部(即在创建GUI其余部分的函数中)访问“文件”。我在启动文件选择器的按钮旁边有一个空白文本标签,因此我要做的是存储文件,并将文本标签的文本设置为文件名
# 1 楼答案
在类级别而不是在anon内部类中定义
File file
变量怎么样