Java 如何在 JFrame 中居中对齐标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9662393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to center align the title in a JFrame?
提问by wishman
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.
我有一个带有 Title的JFrame。我想居中对齐 title,以便它出现在JFrame 的 title 中间。
Thanks.
谢谢。
采纳答案by Java42
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");
回答by blackcompe
I don't know that you can without using a complicated scheme. If it's to your liking, you could apply a titled border with centered text to the frame's content pane.
我不知道你可以不使用复杂的方案。如果您喜欢,您可以将带有居中文本的标题边框应用到框架的内容窗格。
回答by mKorbel
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.
simple not possible to centering narrarive in the JFrames TitleBar
, this container came from Native OS
简单不可能将叙述居中JFrames TitleBar
,这个容器来自Native OS
dirty hack are possible by implements
getSystemLookAndFeel
in Windows OScreate undecorated
JFrame
by addingJPanel
(simulatedToolbar
) with JButtons (JButtons with icons simulating standards buttons )
getSystemLookAndFeel
Windows 操作系统中的工具可以进行脏黑客攻击JFrame
通过添加JPanel
(模拟Toolbar
)与 JButton(带有模拟标准按钮的图标的 JButton)来创建未修饰的
回答by Blastoise Opressor
Adapting the answer's sourceof @Java42, I did a "workaround" to adjust title when resizing JFrame:
调整@Java42的答案来源,我做了一个“解决方法”来调整JFrame大小时调整标题:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
public class JFrameTitleCenter {
public void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(300, 200));
frame.setTitle("Hello Center");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
titleAlign(frame);
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void titleAlign(JFrame frame) {
Font font = frame.getFont();
String currentTitle = frame.getTitle().trim();
FontMetrics fm = frame.getFontMetrics(font);
int frameWidth = frame.getWidth();
int titleWidth = fm.stringWidth(currentTitle);
int spaceWidth = fm.stringWidth(" ");
int centerPos = (frameWidth / 2) - (titleWidth / 2);
int spaceCount = centerPos / spaceWidth;
String pad = "";
pad = String.format("%" + (spaceCount - 14) + "s", pad);
frame.setTitle(pad + currentTitle);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new JFrameTitleCenter().createAndShowGUI();
});
}
}
Working:
在职的: