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

Java 如何在JFrame中居中对齐标题?,java,swing,jframe,titlebar,Java,Swing,Jframe,Titlebar,我有一个带有标题的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. 在JFrames标题栏中不可能将Narrave居中,此容器来自Native

我有一个带有标题的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.
JFrames标题栏中不可能将Narrave居中,此容器来自
Native OS

  • 通过Windows操作系统中的工具
    getSystemLookAndFeel
    可以进行恶意攻击

  • 通过添加带有JButtons(带有模拟标准按钮图标的JButtons)的
    JPanel
    (模拟的
    工具栏
    ),创建未装饰的
    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");
      
      调整@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();
              });
          }
      }
      
      工作:


      在netbeans中,在jFrame中对齐标题非常容易,您需要做的是:

      一,

      二,

    • 运行你的应用程序

    • +1,我想这是正确的答案,但对于resizable from,我想他可以编写一个
      AddWindowsStateListener(…)
      来实现这一点:-)