2012-04-02 70 views
0

我一直试图花几个小时尝试将文本文件的内容加载到JTextArea中。我能够将文本加载到控制台中,但不能加入到JTextArea中我不知道我在做什么错误。任何帮助表示感谢,谢谢!将文本文件加载到JtextArea

的程序

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.io.*; 


public class LoadClass extends JPanel 
{ 
    JPanel cards; 
    private JPanel card1; 
    private JTextArea textarea1; 
    private int currentCard = 1; 
    private JFileChooser fc; 

    public LoadClass() 
    { 
     Font mono = new Font("Monospaced", Font.PLAIN, 12); 

     textarea1 = new JTextArea(); 
     textarea1.setFont(mono); 



     card1 = new JPanel(); 
     card1.add(textarea1); 




     cards = new JPanel(new CardLayout()); 
     cards.add(card1, "1"); 


     add(cards, BorderLayout.CENTER); 



     setBorder(BorderFactory.createTitledBorder("Animation here")); 
     setFont(mono); 
    } 

    public void Open() 
    { 
     textarea1 = new JTextArea(); 


     JFileChooser chooser = new JFileChooser(); 
     int actionDialog = chooser.showOpenDialog(this); 
     if (actionDialog == JFileChooser.APPROVE_OPTION) 
     { 
      File fileName = new File(chooser.getSelectedFile() + ""); 
      if(fileName == null) 
       return; 
      if(fileName.exists()) 
      { 
       actionDialog = JOptionPane.showConfirmDialog(this, 
            "Replace existing file?"); 
       if (actionDialog == JOptionPane.NO_OPTION) 
        return; 
      } 
      try 
      { 

       String strLine; 
       File f = chooser.getSelectedFile(); 
       BufferedReader br = new BufferedReader(new FileReader(f)); 

       while((strLine = br.readLine()) != null) 
       { 
        textarea1.append(strLine + "\n"); 

        System.out.println(strLine); 

       } 
      } 
      catch(Exception e) 
      { 
       System.err.println("Error: " + e.getMessage()); 
      } 
     } 
    } 

} 

类主程序

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class LoadMain extends JFrame 
{ 

    private LoadClass canvas; 

    private JPanel buttonPanel; 
    private JButton btnOne; 



    public LoadMain() 
    { 

     super("Ascii Art Program"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     setLayout(new BorderLayout()); 
     canvas = new LoadClass(); 

     buildButtonPanel(); 

     add(buttonPanel, BorderLayout.SOUTH); 
     add(canvas, BorderLayout.CENTER); 

     pack(); 
     setSize(800, 800); 
     setVisible(true); 

    } 
    private void buildButtonPanel() 
    { 
     buttonPanel = new JPanel(); 

     btnOne = new JButton("Load"); 


     buttonPanel.add(btnOne); 



     btnOne.addActionListener(new btnOneListener()); 


    } 
    private class btnOneListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      if (e.getSource() == btnOne) 
      { 
       canvas.Open(); 
      } 
     } 
    } 


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

} 
+0

尝试将行存储在一个字符串中,然后textArea.setText()? – Giannis 2012-04-02 23:21:28

+0

1)为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 2)请学习常见的[Java命名约定](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307)(具体用于名称的情况)类,方法和属性名称并一致使用它。 – 2012-04-02 23:22:49

+0

这是功课吗? – Peter 2012-04-02 23:34:43

回答

1
public void Open() 
{ 
    textarea1 = new JTextArea(); 

修改成:

public void Open() 
{ 
    //textarea1 = new JTextArea(); // or remove completely 

创建的第二个领域是令人困惑的事情。

+0

你是一个救星! – FireStorm 2012-04-03 02:07:28