0

テキスト ファイルを開いて、その値をString変数に追加して、JTextArea. 私が何をしても、テキストエリアには常に何も返されません。クリアボタンを押したときのようにテキストを変更したいのですが、作成したTest.txtファイルの内容に変更したいです。

package texteditor;

import java.awt.FlowLayout;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class GUI extends JFrame {
    //GUI Variables

    private JButton save;
    private JButton clear;
    private JButton copy;
    private JButton paste;
    private JButton open;
    private JTextArea textField;
    private StringSelection stringSelection;
    private Clipboard clipboard;
    String textFieldWritable;

    public GUI() {
        super("Text Editor");
        //Sets Layout
        setLayout(new FlowLayout());

        save = new JButton("Save");
        add(save);
        clear = new JButton("Clear");
        add(clear);
        open = new JButton("Open");
        add(open);


        textField = new JTextArea(10, 35);
        add(textField);

        handler handle = new handler();

        save.addActionListener(handle);
        clear.addActionListener(handle);

    }

    private class handler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == save) {
                textFieldWritable = String.format("%s", textField.getText());
                try {
                    FileWriter fileWrite = new FileWriter("Text.txt");
                    PrintWriter printWrite = new PrintWriter(fileWrite);

                    printWrite.println(textFieldWritable);
                    printWrite.close();
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null,
                        "Error, File Already Exists!", "Error",
                        JOptionPane.ERROR_MESSAGE);
                }
            } else if (event.getSource() == clear) {
                textField.setText("");
            } else if (event.getSource() == open) {
                try {
                    FileReader fileReader = new FileReader("Text.txt");
                    BufferedReader bufferedReader = new BufferedReader(fileReader);

                    String inputFile = "";
                    String textFieldReadable = bufferedReader.readLine();

                    while (textFieldReadable != null) {
                        inputFile += textFieldReadable;
                        textFieldReadable = bufferedReader.readLine();
                    }
                    System.out.println("inputFile");
                } catch (FileNotFoundException ex) {
                    System.out.println("no such file exists");
                } catch (IOException ex) {
                    System.out.println("unkownerror");
                }
            }
        }
    }
}
4

2 に答える 2

3

私には、読んだテキストをテキスト領域に設定したことがないように見えます。

try {
    FileReader fileReader = new FileReader("Text.txt");
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    String inputFile = "";
    String textFieldReadable = bufferedReader.readLine();

    while (textFieldReadable != null){
        inputFile += textFieldReadable;
        textFieldReadable = bufferedReader.readLine();                    
    }
    System.out.println("inputFile");
    // Opps //
    textField.setText(inputFile);

}
catch (FileNotFoundException ex) {
    System.out.println("no such file exists");
}
catch (IOException ex) {
    System.out.println("unkownerror");
}

個人的には、提供された機能を使用してJTextComponentそれを渡し、Readerそれ自体を読み取れるようにするだけです。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TextAreaReadTest {

    public static void main(String[] args) {
        new TextAreaReadTest();
    }

    public TextAreaReadTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextArea textArea;

        public TestPane() {
            setLayout(new BorderLayout());
            textArea = new JTextArea(10, 80);
            add(new JScrollPane(textArea));
            JButton read = new JButton("Read");
            read.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    Reader reader = null;
                    try {
                        reader = new FileReader(new File("D:/SWANH.txt"));
                        textArea.read(reader, "The force is strong with this one");
                    } catch (Exception exp) {
                        exp.printStackTrace();
                    } finally {
                        try {
                            reader.close();
                        } catch (Exception exp) {
                        }
                    }
                }
            });
            add(read, BorderLayout.SOUTH);
        }

    }

}
于 2013-07-06T03:53:20.660 に答える