我試圖在JFileChooser,以工作來獲取從另一個類我不在乎按鈕看起來怎麼樣選擇的文件選擇SRT文件,但是當有人點擊就可以了。它應該顯示JFileChooser選擇srt文件(如文本文件,但另一種類型),它應該讀取它。JFileChooser的使用和閱讀
這是我的第一類
package AnimeAid;
import java.io.*;
import javax.swing.*;
public class ReadFile {
private File ourFile= null;
private static final JFileChooser selectSrtFile = new JFileChooser();
String filePath = "";
public ReadFile(){
}
public File getSelectFile(){
selectSrtFile.setFileSelectionMode(JFileChooser.FILES_ONLY);
selectSrtFile.showSaveDialog(null);
ourFile = selectSrtFile.getSelectedFile();
filePath = ourFile.getAbsolutePath();
return ourFile;
}
public String readFileInput(){
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getSelectFile()), "UTF-8"));
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
}catch(IOException ex){
return "there is wrong";
}
return "file is added";
}
}
的掃描類
package AnimeAid;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author isslam
*/
public class GuiInterface extends JFrame {
JTable table;
JButton is;
ReadFile t;
public GuiInterface(String title){
setSize(900, 700);
setTitle(title);
setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
is = new JButton();
t = new ReadFile();
Container cp = getContentPane();
cp.add(is);
is.addActionListener(new addButtonWatcher());
}
private class addButtonWatcher implements ActionListener{
@Override
public void actionPerformed(ActionEvent a){
Object buttonPressed=a.getSource();
if(buttonPressed.equals(is))
{
t.getSelectFile();
}
}
}
}
錯誤消息
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: animeaid.GuiInterface
at animeaid.main.main(main.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
它看起來像你使用NetBeans,以便在這些描述你可能會遇到同樣的問題問題:http://stackoverflow.com/questions/20165965/java-runtimeexception-uncompilable-source-code-erroneous-tree-type和http://stackoverflow.com/questions/2333285/java-lang-runtimeexception-uncompilable-源代碼,這是什麼原因。這些問題的答案表明關閉「編譯時保存」,這可能無法解決您的問題,但編譯器可能會向您顯示代碼中的實際錯誤。 – andersschuller
我沒有把它關掉我再給我一次錯誤,但我覺得沒有必要關閉它只需點擊清理並生成但我仍然需要與此問題的幫助 –
沒有其他類傳給你的文件名,或者是你想知道如何使用JFileChooser來獲取文件名(然後讀取文件)? – rogerdpack