2012-11-20 102 views
0

所以,我有這樣的代碼,應該讀回「Questions.txt」的文本,並顯示它在JTextArea中的選項來保存任何編輯,但它只能回讀最後一行。它出什麼問題了?爲什麼我的代碼不能打印出txt文件的所有行?

package secondgui; 

public class MainGUI { 
public static void main (String [] args) { 
    SecondGUI phillip = new SecondGUI(); 
    phillip.repaint(); 

} 
} 




package secondgui; 

import java.awt.event.*; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Scanner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.*; 

public class SecondGUI extends JFrame implements ActionListener { 

/** 
* @param args the command line arguments 
*/ 
private JButton save = new JButton("Save Edits"); 
private JTextArea edit = new JTextArea(); 

public SecondGUI() { 
    this.setVisible(true); 
    this.setSize(600, 600); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 

    JPanel panel = new JPanel(); 
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); 
    this.add(panel); 

    JLabel label = new JLabel("Please edit the question(s) below" 
      + " as you see fit"); 

    panel.add(save); 

    save.addActionListener(this); 


    panel.add(label); 
    edit.setLineWrap(true); 

    try { 
     Scanner b = new readFile().openFile(); 
     while (b.hasNextLine()) { 
      edit.setText(b.nextLine() + "\n"); 
     } 
    } catch (Exception e) {} 

    JScrollPane scrollyBar = new JScrollPane(edit); 
    scrollyBar.setBounds(150, 150, 250, 100); 
    scrollyBar.setSize(300, 300); 

    panel.add(scrollyBar); 
    this.repaint(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == save) { 
     PrintWriter out; 
     FileWriter outFile; 
     try { 
      outFile = new FileWriter("Questions.txt"); 
      out = new PrintWriter(outFile); 
      out.println(edit.getText()); 
      out.close(); 
      outFile.close(); 
     } catch (IOException ex) { 
      Logger.getLogger(SecondGUI.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

} 
} 

package secondgui; 

import java.io.*; 
import java.util.*; 


public class readFile { 

private Scanner x = new Scanner(System.in); 

public Scanner openFile() { 
    try { 
     x = new Scanner(new File("Questions.txt")); 

    } catch (Exception e) { 
     System.out.println("Could not find file"); 

    } 
    return x; 
} 

public void readFile() { 
    while (x.hasNext()) { 
     x.next(); 
    } 
} 

public void closeFile() { 
    x.close(); 
} 
} 
+2

'setText()'方法覆蓋任何先前設置的文本。你爲什麼不給它添加*文本? – BalusC

回答

1

違規行是:edit.setText(b.nextLine() + "\n");您應該追加下一行,而不是設置文本。如果我沒有記錯,可以使用edit.append()方法。

查看JavaDocs瞭解關於JTextArea的更多信息。

0

不管你的最後一個問題,我認爲你的readfile()機制充其量是有問題的。這:

private Scanner x = new Scanner(System.in); 
try { 
     x = new Scanner(new File("Questions.txt")); 

    } catch (Exception e) { 
     System.out.println("Could not find file"); 
    } 

看起來不順眼。如果遇到錯誤,掃描儀默認爲stdin。而這個:

public void readFile() { 
    while (x.hasNext()) { 
     x.next(); 
    } 
} 

似乎只是遍歷文件而沒有做任何事情。我會閱讀tutorial on reading files,並且(至少目前爲止)會拋出明顯的異常,如果其中任何一個失敗。

相關問題