【问题标题】:center align the title in JFrameJFrame中的标题居中对齐
【发布时间】:2015-02-24 20:50:59
【问题描述】:

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

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

【问题讨论】:

  • 您不太可能影响本机提供的框架边框的标题位置,您唯一的其他解决方案是提供自己的外观和感觉框架装饰
  • 为什么要让用户使用一个打破 每个 其他应用程序模式的 GUI。在他们的电脑上?

标签: java swing jframe titlebar


【解决方案1】:

这是在 Swing 中执行此操作的唯一方法。请注意,在窗口标题居中添加空格会影响其他平台。例如,对于 Windows 7,标题显示在左上角,但在 Windows 8 中,标题将显示在顶部中心。除非您想专门检查客户端运行的是什么操作系统,否则我建议您保持原样。

【讨论】:

    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 2012-03-28
      • 1970-01-01
      • 2018-05-01
      • 2014-05-27
      • 2013-10-08
      • 2017-08-28
      • 2017-11-05
      • 2015-07-18
      • 1970-01-01
      相关资源
      最近更新 更多