使用append将文件列表到textarea

时间:2013-09-23 04:41:07

标签: java swing jtextarea

我正在尝试将文件名列入文本区域并遇到问题请帮我摆脱错误

这是代码

public class Bs  extends javax.swing.JFrame
{        
        private javax.swing.JTextArea jt1;

        private javax.swing.JScrollPane j3;

        Action1 action;      


         public Bs()
         {

             initComponents();
             init();


         } 


          private void init()
         {
                  action = new Action1();

        action.loadFiles(jt1);


         }


        private void initComponents()
        {

            getContentPane().setLayout(null);
               //JFrame f = new JFrame("optimized video ................");

               setSize(700,600);
               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                
              JLabel jlabel6 = new JLabel();
              jlabel6.setText("Multimedia resources");
              jlabel6.setBounds(450 , 140, 150, 10);
              getContentPane().add(jlabel6);

              JTextArea jt1 = new JTextArea();
             JScrollPane j3 = new JScrollPane();

            jt1.setColumns(20);
            jt1.setRows(5);

             j3.setViewportView(jt1);
            getContentPane().add(j3);
             j3.setBounds(400, 155, 250, 150);
                     setVisible(true);
          }

public static void main (String args[])
 {
    try 
    {
        javax.swing.UIManager
        .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    java.awt.EventQueue.invokeLater(new Runnable()
    {
            public void run()
            {
                new Bs().setVisible(true);
            }
    });

 }

}

另一段代码Action1.java

 public class Action1 
{
    public static void loadFiles(JTextArea jta)
    {

        File[] file = new File("ServerDB").listFiles();
        for (int i = 0; i < file.length; i++)
        {
                jta.append(file[i].getName() + "\n");


        }

    }

}

当我尝试System.out时,println(file [i] .getName);它在控制台上成功打印但我无法在Textarea中制作

1 个答案:

答案 0 :(得分:0)

检查这些是否有效:

private final static String newline = "\n";

jta.setText(file[i].getText() + newline);