如何在 TitledBorder 中找到标题的 x 位置

How to find the x position of the title in TitledBorder

我试过 getTitlePosition,但它似乎只是 return 边框的起点 0,而不是标题本身。我也尝试了 getBorderJustification,但它 returned 了 Center 的 int 值。

有没有办法获取 TitledBorder 中实际文本的 x 位置?

Box container = Box.createVerticalBox();
Box topBox = new Box(BoxLayout.PAGE_AXIS);
Box btmBox = new Box(BoxLayout.PAGE_AXIS);
TitledBorder border = new TitledBorder("Title");
border.setTitleJustification(TitledBorder.CENTER);

topBox.add(new JLabel(new ImageIcon(new BufferedImage(200,40,BufferedImage.TYPE_INT_RGB))));
btmBox.add(new JLabel(new ImageIcon(new BufferedImage(200,40,BufferedImage.TYPE_INT_RGB))));

topBox.setBorder(border);
btmBox.setBorder(border);

container.add(topBox);
container.add(btmBox);

I couldn't figure out how to ensure that the popup stays above the title if the title is centered

因此您需要检查标题位置。如果它居中那么你使用基础数学:

  1. 你知道面板的宽度
  2. 你知道弹窗的宽度
  3. 从面板宽度中减去弹出宽度并除以 2。