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