Skip to main content
gamedev.net
🔒 Locked

[java] Centering text

Started by Punx 2 replies 400+ views
Original Post
Punx
Punx
I have a String that I want to place in a box in my frame. Now I can manually adjust it until it looks right but then when I put a different sized string of text it''s not centered as it should be. Is there a function that can center text on an x axis? I''ve looked it setHorizontalText or something but I couldn''t get it to work right.
~punx
TheDarkOne
TheDarkOne
If there isn''t one, just get the width and divide by two.
Chaoslab
Chaoslab
If you are using awt you can get a FontMetrics object from the Graphics (Graphics.getFontMetrics()) object and use the FontMetrics.stringWidth(String) to get the width of the string.

Hope that helps....

:-)
ujhkfkfk
Daemon
Daemon
Use a Layout and use the appropriate contraint.

D-

-----------
Érdely!
-----------Érdely!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.