简体   繁体   English

如何将Text对象居中到JavaFX Stage的确切中心?

[英]How do I center a Text object to the exact center of a JavaFX Stage?

I am trying to align the exact center of a Text object in JavaFX, and have it sit in the exact center of the JavaFX window. 我正在尝试在JavaFX中对齐Text对象的确切中心,并将其放在JavaFX窗口的确切中心。 This part works fine. 这部分工作正常。 Whenever I add in another Text object that reads 'SETTINGS', I use the same formula, but I change the y-axis by 10 pixels. 每当我添加另一个读取“设置”的Text对象时,我都使用相同的公式,但是我将y轴更改了10个像素。 For some reason, the second text aligns to the right of the other text, while keeping the changed y-axis. 由于某种原因,第二个文本在保持更改后的y轴的同时,与另一个文本的右边对齐。 Why is this? 为什么是这样?

This is my formula: (widthOfWindow / 2) - (widthOfTextObject / 2) 这是我的公式:(widthOfWindow / 2)-(widthOfTextObject / 2)

This should give me the exact center of the the x-axis, right? 这应该给我x轴的精确中心,对吗? I simply change it to fit the y-axis. 我只是将其更改为适合y轴。

EDIT1: This link is a picture of the method I'm using, as well as the product. EDIT1:此链接是我正在使用的方法以及产品的图片。

It turns out that my font doesn't have the same width across all it's characters, preventing me from accomplishing it. 事实证明,我的字体的所有字符宽度都不相同,这使我无法做到这一点。 I've picked another font to use, and it works brilliantly. 我选择了另一种字体来使用,效果很好。 Thanks guys!! 多谢你们!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM