使用Java drawString实现以下Text对齐 [英] Use Java drawString to achieve the following Text alignment

查看:777
本文介绍了使用Java drawString实现以下Text对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用Java2D drawString来实现以下外观。

I wish to use Java2D drawString to achieve the following looks.

但是,我知道如何实现以下文本对齐?

However, I have 0 idea how I an achieve the following text alignment?

正如我们所看到的,日期:,开放:,......都是左对齐。

As we can see, "Date:", "Open:", ... are all being aligned to left.

并且30-Nov-09,1262.000,......都正确对齐。

And "30-Nov-09", '1262.000", ... are all being aligned to right.

替代文字http://sites.google.com/site/yanchengcheok/Home/drawstring.png

推荐答案

要右对齐文本,您可以计算出正在渲染的文本的宽度,然后从x坐标中减去该宽度。例如:

To right-align text you can figure out the width of the text you're rendering, and then subtract that width from the x-coordinate. eg:

g.drawString(s, rightEdge - fontMetrics.stringWidth(s), y);

这篇关于使用Java drawString实现以下Text对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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