如何将txt文件的内容读取到文本区域?

时间:2017-10-02 09:00:48

标签: java swing jtextarea

我想将文本文件的内容读入文本区域 应用。文本区域显示为空,我无法在其中键入任何内容。我是Swing的新手。

这是我使用的代码:

class Menu implements ActionListener
{
    JLabel jlab;

    Menu()
    {
        JFrame jfrm = new JFrame("Menudemo");
        jfrm.setLayout(new FlowLayout());
        jfrm.setSize(720, 700);

        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
         JTextArea edit = new JTextArea("HI I AM A TEXTAREA",520,500);
        edit.setEditable(true);
        JScrollPane sta = new JScrollPane(edit);

        sta.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        sta.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        jlab = new JLabel();

        JMenuBar jmb = new JMenuBar();

        JMenu jmfile = new JMenu("File");
        JMenuItem jmiopen = new JMenuItem("Open");
        JMenuItem jmiclose = new JMenuItem("Close");
        JMenuItem jmisave = new JMenuItem("Save");
        JMenuItem jmiexit = new JMenuItem("Exit");
        jmfile.add(jmiopen);
        jmfile.add(jmiclose);
        jmfile.add(jmisave);
        jmfile.add(jmiexit);
        jmb.add(jmfile);

   //i want to open a file through the dialog box and load the content into the 
   //text area
        jmiopen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae)
            {
                 try{
                      File selectedFile = jfc.getSelectedFile();

                        int returnvalue = jfc.showOpenDialog(null);

                        if(returnvalue == JFileChooser.APPROVE_OPTION)
                        {
                        FileReader reader = new 
                        FileReader(selectedFile.getAbsolutePath());

                        @SuppressWarnings("resource")
                        BufferedReader br = new BufferedReader(reader);
                        String line = br.readLine();
                        while(line != null)
                        {
                            edit.append(line);
                           line=br.readLine();
                        }
                        /*ja.read(br,null);
                        br.close();
                        ja.requestFocus();
                        */
                       }
                 }
                 catch(Exception e2)
                    {
                        System.out.println(e2);
                    }
             }
        });
        jmiclose.addActionListener(this);
        jmisave.addActionListener(this);
        jmiexit.addActionListener(new ActionListener(){
            @Override
             public void actionPerformed(ActionEvent ae){
                System.exit(0);
            }
        });
        jfrm.add(edit);
        jfrm.add(sta);

        jfrm.add(jlab);
        jfrm.setJMenuBar(jmb);
        jfrm.setVisible(true);
  }

    public static void main(String args [])
    {
         SwingUtilities.invokeLater(new Runnable(){
             public void run()
             {
                 new Menu();
             }
         });
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
    }
    }

1 个答案:

答案 0 :(得分:1)

以下代码可以使用。您的第一个问题是在用户可以选择任何内容之前获得selectedFile(将其移动到用户选择文件的位置)。你的下一个问题是布局。作为入门者,您应该从BorderLayout开始,因为它很容易处理,直到您获得概念。第三个问题是您添加了TextArea 2次。第一次使用jfrm.add(edit);,第二次使用jfrm.add(sta);。您只需添加ScrollPane,因为它包含TextArea

package test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileSystemView;

class Menu implements ActionListener {
    public Menu() {
        JFrame jfrm = new JFrame("Menudemo");
        jfrm.setLayout(new BorderLayout());
        jfrm.setSize(720, 700);

        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
        JTextArea edit = new JTextArea("HI I AM A TEXTAREA");
        edit.setEditable(true);
        JScrollPane sta = new JScrollPane(edit);

        sta.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        sta.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        JMenuBar jmb = new JMenuBar();

        JMenu jmfile = new JMenu("File");
        JMenuItem jmiopen = new JMenuItem("Open");
        JMenuItem jmiclose = new JMenuItem("Close");
        JMenuItem jmisave = new JMenuItem("Save");
        JMenuItem jmiexit = new JMenuItem("Exit");
        jmfile.add(jmiopen);
        jmfile.add(jmiclose);
        jmfile.add(jmisave);
        jmfile.add(jmiexit);
        jmb.add(jmfile);

        // i want to open a file through the dialog box and load the content into the
        // text area
        jmiopen.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                try {

                    int returnvalue = jfc.showOpenDialog(null);

                    if (returnvalue == JFileChooser.APPROVE_OPTION) {
                        File selectedFile = jfc.getSelectedFile();
                        try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) {
                            String line = reader.readLine();

                            while (line != null) {
                                edit.append(line);
                                line = reader.readLine();
                            }
                            //Suggestion opposed by Andrew Thompson; 
                            //would be used instead of while loop. 
                            //It will override any existing text 
                            //with the whole content of the file
                            edit.read(reader, selectedFile);

                        }
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        });
        jmiclose.addActionListener(this);
        jmisave.addActionListener(this);
        jmiexit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });

        jfrm.add(sta);
        jfrm.setJMenuBar(jmb);
        jfrm.setVisible(true);
        // jfrm.pack();

    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Menu();

            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}