java - JFrame 中的标题居中对齐

标签 java swing jframe titlebar

我想知道如何使 JFrame 标题居中。我引用了以下链接 How to center align the title in a JFrame?

但我需要将标题居中而不留间距。

最佳答案

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

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");

https://stackoverflow.com/a/9662974/3675925

关于java - JFrame 中的标题居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27675814/

相关文章:

java - 你会如何递归地编写这段代码?

java - 将不同对象类型的数组列表合并为一个

java - 如何在 Java Swing 中设置当前屏幕尺寸?

java - 哪些 Swing 容器可以设置为可聚焦?

java - 根据JFrame Resize控制JPanels大小

Java JFrame 方法 pack()

java - 如何设置我在加载其他 jframe 类时 setvisible(false) 的 JFrame?

java - 无法访问类 jdk.xml.internal.JdkXmlUtils

java - Hadoop 独立模式给出连接错误

java - 更新 Java 中的现有变量