2014-01-14 36 views
0

我試圖在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) 
+0

它看起來像你使用NetBeans,以便在這些描述你可能會遇到同樣的問題問題:http://stackoverflow.com/questions/20165965/java-runtimeexception-uncompilable-source-code-erroneous-tree-type和http://stackoverflow.com/questions/2333285/java-lang-runtimeexception-uncompilable-源代碼,這是什麼原因。這些問題的答案表明關閉「編譯時保存」,這可能無法解決您的問題,但編譯器可能會向您顯示代碼中的實際錯誤。 – andersschuller

+0

我沒有把它關掉我再給我一次錯誤,但我覺得沒有必要關閉它只需點擊清理並生成但我仍然需要與此問題的幫助 –

+0

沒有其他類傳給你的文件名,或者是你想知道如何使用JFileChooser來獲取文件名(然後讀取文件)? – rogerdpack

回答

1

它看起來像你的兩個類有不同的封裝:

package animeFactor; 
      ^

package animefactor; 
      ^

我相信Java包名稱是區分大小寫的,所以儘管你有(可能)把兩個文件在同一文件夾中,GuiInterface類不能使用ReadFile類無進口它。

要麼改變包的定義是相同的,或添加一個import語句以GuiInterface

import animeFactor.ReadFile; 
+0

這是正確的謝謝,我會更新的問題,所以你可以採取固定問題後看,因爲它不是唯一的一個 –

+0

拿來看的彙編現在的問題是不是與包名存實亡它與代碼還比如,我可以寫空承包商 –

+0

我覺得應該是掃描儀類 –