java print api,Java Print API-使用等距的“ Courier New”字体错误地打印了空格字符

Let me first describe the picture below:

There are two printed papers. The only difference between them is that few space " " characters from paper in left are replaced by dot "." character in the paper in right.

Red line represents the left border to which the text should be aligned

Green curve represents my intention to align all characters it connects into a single column. In fact the green curve is supposed to be a vertical line.

1xAPo.jpg

I want all characters highlighted by the green line to be printed in one column.

Font of a String is monospaced Courier New. However, it seems that space characters are not printed as monospaced (see 'dotted' lines vs. lines with space characters at the beginning).

To print the string I use standard Java Print Service API over JTextPane component:

PrinterJob pj = PrinterJob.getPrinterJob();

pj.setPrintable(myTextPane);

pj.print();

To my knowledge the Java Print Service API in fact calls paint() methods of myTextPane.

Therefore the preview should look exactly the same as the printed version of String.

However, it doesn't. The preview does not seem to misinterpret monospaced space characters (see the last picture). The preview looks exactly as I want the text to be printed.

4U2rd.png

Any suggestions how to force JavaPrintServiceAPI to print monospaced space characters correctly?

解决方案

I don't think you can fix this in the printing API.

First divide each line after the "green" digit into a left and (possibly empty) right substring.

In your JTextArea, use align to justify a two-column HTML table, as described in How to Use HTML in Swing Components.

As an alternative to JTextPane, use a GridLayout of JLabel. Give the left column RIGHT_ALIGNMENT and the right column LEFT_ALIGNMENT.

A two-column JTable, which uses JLabel for rendering, might be a third alternative. See How to Use Tables—Concepts: Editors and Renderers for details.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值