1
我正在创建一个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个字符的宽度。但是,它开始偏离列式方面。
我该怎么做才能使其对齐?
我想在'JtextArea'中输出值的文本表格表示形式,它是一个特定程序的记录器。 – Dinesh
那么我会推荐使用固定宽度的字体。我修改了我的答案。 ''monospaced''将使用系统默认的固定宽度字体。 – NighttimeDriver50000
@ NighttimeDriver50000:谢谢!有用.. – Dinesh