简体   繁体   中英

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. 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. For some reason, the second text aligns to the right of the other text, while keeping the changed y-axis. Why is this?

This is my formula: (widthOfWindow / 2) - (widthOfTextObject / 2)

This should give me the exact center of the the x-axis, right? I simply change it to fit the y-axis.

EDIT1: This link is a picture of the method I'm using, as well as the product.

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!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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