以表格格式格式化JTextArea

时间:2013-12-25 13:42:33

标签: java swing jtextarea japplet string-formatting

我正在JTextAreaJApplet做简单的文字报道。

我正在尝试显示这个       Table格式的String数组,它运行良好,但我希望Integer数字应该在RightAlign中,文本应该在LeftAlign.as中,请建议我JTextArea好或{ {1}}是?

制作简单的文本报告。我的下一个目标是从XML文件中获取输入并按表格结构显示,我知道它太难以制作这样的东西,但他们所说的“没有什么是不可能的”。所以有人帮助我这样做,我完全坚持这一点。

JTextpane

2 个答案:

答案 0 :(得分:5)

使用JTable表格数据!有关详细信息,请参阅How to Use Tables

A JTable

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 java.util.MissingFormatArgumentException;

import javax.swing.*;
import javax.swing.border.Border;

public class MainReport extends JApplet {
    String[] colName = new String[] { "Date", "Account.No", "Description",
            "Deposit", "Withdraw" };
    // here is my input of String Array.
    String a[][] = new String[][] {
            { "13/12/2013", "101", "AlphaSoftInfotekNashik", "3000", "0" },
            { "15/12/2013", "102", "Bank Ladger 2 xxxxxxxxxxxxx", "5000", "0" },
            { "16/12/2013", "103", "Accout Closing with Details", "800", "0" } };
    StringBuilder sb = new StringBuilder();
    final String Header = "%s %85s%n%n%s %10s%n";
    // final String HRule ="";
    final String format = "|%1$-30s|%2$-30s|%3$-30s|%4$-30s|%5$-35s";
    final String NumColF = "";
    String cformat = "%20s";
    String format1 = "%20s";
    String s1 = "%61s";// for spillover text
    String s2 = "%10s";
    int i, j, k;
    int Position = 0;
    boolean flag = false;
    String a1, a2;
    Container c;

    JTable outputArea;
    public static final int LINES = 10;
    public static final int CHAR_PER_LINE = 40;
    JButton b;

    public void init() {
        c = getContentPane();
        c.setLayout(new FlowLayout());
        outputArea = new JTable(a, colName);
        //outputArea.setLocation(100, 100);  // pointless when using layout managers..
        Border border = BorderFactory.createLineBorder(Color.RED);
        outputArea.setBorder(BorderFactory.createCompoundBorder(border,BorderFactory.createEmptyBorder(10, 10, 10, 10)));
        c.add(outputArea);
        b = new JButton("Show Report");
        c.add(b);
    }
}

答案 1 :(得分:0)

您可以使用JEditorPane而不是JtextArea。 你在那里使用

    SetContentType("text/html");

方法然后使用html表来显示数据。

http://www.w3schools.com/html/html_tables.asp

你也可以使用制表符'/ t'将单词分开看起来像一个表并给出一个'|'在他们之间...