JTextArea输出上的String数组应采用表格形式

时间:2013-12-21 13:52:54

标签: java arrays string jtextarea

我需要帮助 我尝试使用此程序在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]));    
                    }
                }    
            }
        }          
    }    
}

1 个答案:

答案 0 :(得分:1)

我将另一个outputArea.append("\n");放在外部for循环的最后,它似乎有效。您只需要修复数据的格式间距

编辑:试一试。只需使用format变量

即可
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));
    }

}