考虑将标题左对齐…但是…这将使您靠近中心。对于可调整大小的框架,您需要在调整大小时重写标题。
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");


