Java JTextArea输出上的字符串数组应为表格形式

Java JTextArea输出上的字符串数组应为表格形式,java,arrays,string,jtextarea,Java,Arrays,String,Jtextarea,我需要帮助 我尝试了这个程序,在JTextArea上用简单的打印制作报告 输入是在简单的字符串数组中,所以我必须打印它或以表格形式显示它 它看起来像这样: ----------------------------- |col1|col2|this is some | | | |very long | | | |Text Here! | ----------------------------- 提前谢谢 package reportla

我需要帮助 我尝试了这个程序,在JTextArea上用简单的打印制作报告 输入是在简单的字符串数组中,所以我必须打印它或以表格形式显示它 它看起来像这样:

-----------------------------
|col1|col2|this is some     |
|    |    |very long        |
|    |    |Text Here!       |
-----------------------------
提前谢谢

package reportlast;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import static reportlast.Padding.padLeft;

    public class ReportLast extends JApplet implements ActionListener {
        String[] colName = new String[]           {"Date","Account.No","Description","Deposit","Withdraw"};

        String A[][] = {{"13/12/2013", "101", "AlphaSoftInfotekNashik", "3000", "0"},
                   {"15/12/2013", "103", "Bank Ladger 2 xxxxxxxxxxxxx", "5000", "0"}};
        String[][] B = new String[3][5];

        Container c;
         JTextArea outputArea;
        //JScrollPane js1;    
        JButton b;

        public void init() {    
        c = getContentPane();
        c.setLayout(new FlowLayout());    
        outputArea = new JTextArea(20, 80);
        Border border = BorderFactory.createLineBorder(Color.RED);
        outputArea.setBorder(BorderFactory.createCompoundBorder(border,
                BorderFactory.createEmptyBorder(10, 10, 10, 10)));
        c.add(outputArea);
        //js1=new JScrollPane(outputArea);
       // add(js1);
        b = new JButton("Show Report");
        b.addActionListener(this);
        c.add(b);

       }
        public void actionPerformed(ActionEvent e) {
        outputArea.setEditable(false);
        outputArea.setFont(new Font("Times New Roman",Font.PLAIN,14));
        outputArea.setText(" ");
        outputArea.append("ALPHASOFT -- BANK LADGER , REPORTS.\n\n");
        outputArea.append("Sr.No");
        for(int j=0;j<colName.length;j++)
        {
            outputArea.append("\t"+colName[j]);                
        }          
        outputArea.append("\n");    
        for (int i = 0; i < A.length; i++) {
            //someLine = String.format(format);
            for (int j = 0; j < A[i].length; j++) {
                B[i][j] = A[i][j];    
                while (B[i][j] != null) {
                    A[i][j] = B[i][j];
                    B[i][j] = null;    
                    if (A[i][j].length() > 20){
                        int split = 20;
                        for (int n = 20; n > 0; n--) {
                            String bc = A[i][j].substring(n, n + 1);
                            char result = A[i][j].charAt(n);    
                            if (result != ' ') {

                            } else {
                                split = n + 1;    
                                break;
                            }    
                        }
                            String s =String.valueOf(A[i][j].substring(0, split));
                            if(s!=null)
                            {                                    
                                outputArea.append(s);                                
                            }
                        //outputArea.append(s);    
                        B[i][j] = A[i][j].substring(split, A[i][j].length());    
                    } else {
                        // outputArea.append(j + 1);
                        outputArea.append(String.valueOf("\t"+A[i][j]));    
                    }
                }    
            }
        }          
    }    
}
我放置了另一个outputArea.append\n;在外部for循环的最末端,它似乎可以工作。您只需要固定数据的格式间距

编辑:试试这个。只需使用format变量即可


你有什么问题吗?我自己试过上面的代码,但它不是表格格式的,所以请帮我做点什么实际上我的老板在这项工作上完全把我吸引住了,他告诉我不要使用JTable,甚至\r\n\t!谢谢您的帮助,但它只适用于表格结构的标题。我需要有关在上述程序中显示rowXcolumn的帮助,所以请帮助我解决此问题。@peeskillet
protected final String format = "%-20s%-20s%-45s%-20s%-20s\n";

public void actionPerformed(ActionEvent e) {
    outputArea.setEditable(false);
    outputArea.setFont(new Font("Times New Roman", Font.PLAIN, 14));
    outputArea.setText(" ");
    outputArea.append("ALPHASOFT -- BANK LADGER , REPORTS.\n\n");
    outputArea.append("Sr.No");
    outputArea.append("\n");

    String date;
    String account;
    String desc;
    String depos;
    String withd;

    date = colName[0];
    account = colName[1];
    desc = colName[2];
    depos = colName[3];
    withd = colName[4];

    outputArea.append(String.format(format, date, account, desc, depos, withd));


    for (String[] A1 : A) {
        date = A1[0];
        account = A1[1];
        desc = A1[2];
        depos = A1[3];
        withd = A1[4];
        outputArea.append(String.format(format, date, account, desc, depos, withd));
    }

}