我正在尝试创建一个xml文件阅读器。我在JFrame中使用eclipse制作了主要的xml文件,并编写了如下文件阅读器代码;
public xml() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 486, 533);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnopen = new JButton("Open");
btnopen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int returnVal = fc.showOpenDialog(contentPane);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try
{
File file = fc.getSelected();
textFirst.setText(file.getAbsolute("XML", "xml"));
StaxParser read = new StaxParser();
List<Student> readStudents = read.readStudents(file.getAbsolutePath());
for (Student student : readStudents) {
textOutput.append(student+"\n\n");
}
}
catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
textOutput.append("\nError");
}
} else {
textOutput.setText("user cancelled operation");
}
}
});
我在它所说的位上收到错误;
File file = fc.getSelected();
我得到的错误就是这个;
http://gyazo.com/10d739192c178e04a085bd392e93139b
答案 0 :(得分:0)
我认为你错了尝试
File file = fc.getSelectedFile()
答案 1 :(得分:0)
根据链接中的例外情况,您缺少一些导入,例如:
File cannot be resolved to a type
缺失
import java.io.File;
List cannot be resolved to a type
缺失
import java.util.List;
等。 尝试为该异常中提到的所有类添加正确的导入。
而且,正如@Gaurav Joseph所提到的,你使用错误的方法获取文件。