如何使用textarea在jfram中显示.dat文件中的信息

时间:2016-01-18 02:15:18

标签: java eclipse jtextarea

我正在尝试创建一个数据库。 但每当我尝试显示我的日志文件时,都没有任何反应。 框架打开但除了我的按钮和滚动条之外什么都没有。 一切都运行但它不会显示我在.dat文件中的内容。 如何将我的JTextArea从我的公共空间带到我的主要? 我也正确地从文件中读取? 还有其他方法我应该这样做吗?

public class displaylog extends JFrame {

 public static void main(String[] args) throws IOException {
     // TODO Auto-generated method stub
    displaylog newerconcoles = new displaylog();
    EventQueue.invokeLater(new Runnable() {
         public void run() {

            try {
                displaylog frame = new displaylog();
                frame.getContentPane();
                frame.setVisible(true);

                final JTextArea edit = readFromFile();

                frame.getContentPane().add(edit);
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

     });

  }

  public displaylog() throws IOException {
    setIconImage(Toolkit.getDefaultToolkit().getImage(
            "H:\\Downloads\\potato-03.jpg"));
    setTitle("Inventory 3000 and 1");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 616, 447);
    getContentPane().setLayout(null);

    JButton btnMainMenu = new JButton("Main Menu");
    btnMainMenu.setBounds(250, 386, 113, 23);
    getContentPane().add(btnMainMenu);
    btnMainMenu.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            newframe regFace = new newframe();
            regFace.setVisible(true);
            dispose();
        }
     });

    JScrollBar scrollBar = new JScrollBar();
    scrollBar.setBounds(582, 38, 16, 272);
    getContentPane().add(scrollBar);
 }

  public static JTextArea readFromFile() {
    String fileName = "MasterLog.dat";
    final JTextArea edit = new JTextArea(40, 60);
    edit.setBackground(Color.RED);
    edit.setEditable(false);
    edit.setBounds(584, 43, -556, 272);
    try {
        Charset charset = Charset.defaultCharset();
        String[] lines = Files.readAllLines(new File(fileName).toPath(),
                charset).toArray(new String[0]);

        for (int i = 0; i < lines.length; i++) {
            edit.append(lines[i]);
            System.out.println(lines[i]);
            edit.append("\n");
        }

    } catch (IOException e) {
        System.out.println("File Not Found (Check Path)");

    }
    return edit;

    }
 }

0 个答案:

没有答案
相关问题