简体   繁体   中英

Applet - align text to bottom middle

Is there a way to align text from g.drawString() to the bottom center of a java applet?

I am also hoping for a way that is fluid between full-screen and small screen.

Yes, start by taking a look at Measuring Text

String text = "Happy at the bottom";
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = (getHeight() - fm.getHeight()) + fm.getAscent();
g.drawString(text, x, y);

The next question that needs to asked, is could what you want be achieved by using a JLabel and a BorderLayout (or other compound layout)....?

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