对齐JTextArea中的文本表格表示

时间:2016-03-09 06:20:20

标签: java jtextarea text-alignment

我正在创建一个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);
    }
}

输出

Java GUI Output

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

实际数据:

"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\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个字符。但是,它开始偏离列式方面。

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

1 个答案:

答案 0 :(得分:3)

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

如果你真的需要文字区域,我建议:

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

这将使其显示在代码编辑器中。