java 讀取顯示txt內容(Swing)

java 讀取顯示txt內容(Swing),JTextAreajava

public class TxtSwing extends JFrame {

    private JTextArea textAreaOutput;

    public TxtSwing() throws IOException {
        super("txt");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.FlowLayout());
        textAreaOutput = new JTextArea("111", 45, 80);
        textAreaOutput.setSelectedTextColor(Color.RED);
        textAreaOutput.setLineWrap(true); // 激活自動換行功能
        textAreaOutput.setWrapStyleWord(true); // 激活斷行不斷字功能
        BufferedReader reader = new BufferedReader(new FileReader(
                "test.txt"));
        String str = reader.readLine();
        String end = null;
        while ((str != null)) {
            end = end + str + "\n";
            str = reader.readLine();
        }
        textAreaOutput.setText(end);

        getContentPane().add(textAreaOutput);
        setSize(1024, 768);

    }

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        TxtSwing tm = new TxtSwing();
        tm.setVisible(true);
    }

}
相關文章
相關標籤/搜索