如何在JFrame中居中对齐标题?

编程入门 行业动态 更新时间:2024-10-28 21:15:32
本文介绍了如何在JFrame中居中对齐标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有标题的 JFrame .我希望标题居中对齐,以便它出现在JFrame标题的中间.

I have a JFrame with a Title. I want center align the title, so that it appears in the middle of the JFrame's title.

谢谢.

推荐答案

考虑将标题左对齐...但是...这将使您靠近中心.对于可调整大小的框架,您需要在调整大小时重写标题.

Consider leaving the title left-justified...but...this will get you near the center. For resizable frames, you need to rewrite the title on resize.

JFrame t = new JFrame(); t.setSize(600,300); t.setFont(new Font("System", Font.PLAIN, 14)); Font f = t.getFont(); FontMetrics fm = t.getFontMetrics(f); int x = fm.stringWidth("Hello Center"); int y = fm.stringWidth(" "); int z = t.getWidth()/2 - (x/2); int w = z/y; String pad =""; //for (int i=0; i!=w; i++) pad +=" "; pad = String.format("%"+w+"s", pad); t.setTitle(pad+"Hello Center");

更多推荐

如何在JFrame中居中对齐标题?

本文发布于:2023-10-07 22:24:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标题   如何在   JFrame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!