对齐 JTextArea 中的文本表格表示

Aligning the textual tabular representaion in JTextArea

我正在创建一个 JTextArea 并向其附加一些换行符分隔的字符串。

它看起来与输入字符串中的实际排列方式不同。

JTextAreaDemo.java

import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 *
 * @author dinesh
 */
public class TextAreaDemo {

    public static void main(String[] args) {
        String input
                = "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
                + "| Name               | STC Port           | Tx Count (frames)  | Rx Count (frames)  | Tx Rate (fps)      | Rx Rate (fps)      | Tx Count (bits)    | Rx Count (bits)    | Tx Rate (bps)      | Rx Rate (bps) |    \n"
                + "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
                + "| vlan105            | 12/5               | 165                | 146                | 5                  | 5                  | 168960             | 149504             | 5120               | 5120 |             \n"
                + "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
                + "| vlan104            | 12/5               | 165                | 145                | 5                  | 5                  | 168960             | 148480             | 5120               | 5120 |             \n"
                + "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
                + "| vlan105            | 12/6               | 159                | 146                | 5                  | 5                  | 162816             | 144832             | 5120               | 4960 |             \n"
                + "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
                + "| vlan104            | 12/6               | 158                | 145                | 5                  | 5                  | 161792             | 143840             | 5120               | 4960 |             \n"
                + "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";

        JFrame myFrame = new JFrame("Text");
        JPanel pnlMain = new JPanel(new BorderLayout());
        JTextArea txtArea = new JTextArea();
        pnlMain.add(txtArea);
        txtArea.setEditable(false);
        myFrame.getContentPane().add(pnlMain);
        myFrame.setSize(400, 400);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
        txtArea.append(input);
    }
}

输出:

如您所见,文本的对齐方式看起来与实际数据不同。

实际数据:

"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
"| Name               | STC Port           | Tx Count (frames)  | Rx Count (frames)  | Tx Rate (fps)      | Rx Rate (fps)      | Tx Count (bits)    | Rx Count (bits)    | Tx Rate (bps)      | Rx Rate (bps) |    \n"
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
"| vlan105            | 12/5               | 165                | 146                | 5                  | 5                  | 168960             | 149504             | 5120               | 5120 |             \n"
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
"| vlan104            | 12/5               | 165                | 145                | 5                  | 5                  | 168960             | 148480             | 5120               | 5120 |             \n"
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
"| vlan105            | 12/6               | 159                | 146                | 5                  | 5                  | 162816             | 144832             | 5120               | 4960 |             \n"
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
"| vlan104            | 12/6               | 158                | 145                | 5                  | 5                  | 161792             | 143840             | 5120               | 4960 |             \n"
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"

我放置字符串的方式是每个单元格有 20 个字符宽度。但是,它开始偏离列方面。

我应该怎么做才能使其相应对齐?

出现这种情况是因为文本区域中的比例宽度字体和代码编辑器中的固定宽度字体。看起来你真正想要的是 JTable.

如果你真的需要一个文本区域,我会推荐:

txtArea.setFont(new Font("monospaced", Font.PLAIN, 12));

这将使它像在您的代码编辑器中一样显示。