将文本与 Java Graphics 2d 对齐
谁能告诉我如何在 Java 2d 中直接放置文本?
这是代码,它绘制一列自然左对齐的文本。
Font yFont = new Font("Arial", Font.BOLD, 13);
interval = 0;
g2d.setFont(yFont);
for (String l : binLabels) {
g2d.drawString(l, 0, (135 + interval));
interval = interval + 15;
}
让我发疯。 谢谢大家
懒惰类型
Can anyone tell me how to alight text right in Java 2d?
Here's the code, it draws a column of text that is naturally aligned left.
Font yFont = new Font("Arial", Font.BOLD, 13);
interval = 0;
g2d.setFont(yFont);
for (String l : binLabels) {
g2d.drawString(l, 0, (135 + interval));
interval = interval + 15;
}
Driving me crazy.
Thanks y'all
slothishtype
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 PaintComponent() 方法中,您可以使用 FontMetrics 来获取要绘制的字符串的宽度:
然后根据组件的宽度计算开始绘制的偏移量。
问题是你为什么要尝试这样做。您可以只使用 JLabel 并将其对齐设置为右侧。
In your paintComponent() method you can use the FontMetrics to get the width of the string you want to paint:
Then you calculate the offset where to start painting based on the width of the component.
The question is why are you trying to do this. You can just use a JLabel and set its alignment to the right.