我需要在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);
解决方法:
HTML在JTextPane而不是JTextArea中,可以使用HTML:
Description | .... |
---|---|
... 使用CSS样式最漂亮,需要一些努力. String.format在JTextArea中使用固定大小的字体(等距)可以做到: txtArea.setFont(new Font("monospaced", Font.PLAIN, 12)); txtArea.append(String.format("%-30s %15s %10d\n", desc, price, quanty)); >%-30s左对齐字符串 >?s右对齐字符串 >?d右对齐整数 标签:java,swing 来源: https://codeday.me/bug/20191013/1907182.html |