我需要在JtextArea中添加项目及其描述和价格,但我在调整它时遇到问题。我想要一个输出就像
Description Price
Coffe $50.00
Bread $30.00
Egg $10.00
但我的程序就像
一样Description Price
Coffe $50
Bread $30
Egg $20
任何人请帮助我如何对齐它?....
txtArea.append( "\n" + desc+" " + price + " " + quanty);
答案 0 :(得分:3)
HTML 在JTextPane而不是JTextArea中,可以使用HTML:
<html><table><tr><th>Description</th><th>....<tr><td>...
CSS样式最美,需要一些努力。
String.format 在JTextArea中使用固定大小的字体(等宽字体)可以执行以下操作:
txtArea.setFont(new Font("monospaced", Font.PLAIN, 12));
txtArea.append(String.format("%-30s %15s %10d\n", desc, price, quanty));