在java中对齐JtextArea中的文本 [英] Aligning Text In JtextArea in java

查看:29
本文介绍了在java中对齐JtextArea中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 JtextArea 中添加项目及其描述和价格,但我在对齐时遇到问题.我想要这样的输出

I need to add items in a JtextArea and with its Description and a Price but i have a problem in aligning it. i want an output of this is like

Description          Price
Coffe                $50.00
Bread                $30.00
Egg                  $10.00

但我的程序就像

Description           Price
Coffe           $50
Bread                 $30
Egg                 $20

有人请帮助我如何对齐吗?....

Anyone pls help me how to Align it?....

txtArea.append( "\n" + desc+"         " + price + "         " + quanty);

推荐答案

HTML 在 JTextPane 而不是 JTextArea 中可以使用 HTML:

HTML In a JTextPane rather than JTextArea one could use HTML:

<html><table><tr><th>Description</th><th>....<tr><td>...

CSS 样式最漂亮,需要一些努力.

With CSS styles the most beautiful, requiring some effort.

String.format 在 JTextArea 中使用固定大小的字体(等宽)可以:

String.format With a fixed-size font (monospaced) in a JTextArea one can do:

txtArea.setFont(new Font("monospaced", Font.PLAIN, 12));
txtArea.append(String.format("%-30s %15s %10d\n", desc, price, quanty));

  • %-30s 左对齐字符串
  • %15s 右对齐字符串
  • %10d 右对齐整数
  • 这篇关于在java中对齐JtextArea中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆