0

したがって、「Questions.txt」からテキストを読み戻し、編集を保存するオプションを使用して JTextArea に表示することになっているこのコードがありますが、最後の行のみを読み戻します。どうしたの?

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();
}
}
4

2 に答える 2

1

問題のある行は次のとおりedit.setText(b.nextLine() + "\n"); です。テキストを設定するのではなく、次の行を追加する必要があります。私の記憶が正しければ、edit.append()使用できる方法があります。

の詳細については、JavaDocsJTextAreaを参照してください。

于 2012-11-20T17:46:55.373 に答える
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");
    }

ダサく見える。エラーが発生した場合、スキャナーはデフォルトで標準入力になります。この:

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

何もせずにファイルを反復しているようです。ファイルの読み取りに関するチュートリアルを読み、(少なくとも現時点では)失敗した場合は明示的な例外をスローします。

于 2012-11-20T17:50:02.547 に答える