简体   繁体   English

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

[英]How to find the x position of the title in TitledBorder

I've tried getTitlePosition, but it only seems to return the border's starting point, 0, not the title itself. 我已经尝试过getTitlePosition,但是它似乎只返回边框的起点0,而不是标题本身。 I also tried getBorderJustification, but it returned the int value for Center. 我也尝试过getBorderJustification,但是它返回了Center的int值。

Is there a way to get the x position for the actual text in a TitledBorder? 有没有办法获得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 如果标题居中,我不知道如何确保弹出窗口保持在标题上方

So you will need to check the title location. 因此,您需要检查标题位置。 if it is centered then you use basic math: 如果居中,则使用基本数学:

  1. you know the width of the panel 你知道面板的宽度
  2. you know the width of the popup 你知道弹出窗口的宽度
  3. subtract the popup width from the panel width and divide by 2. 从面板宽度中减去弹出窗口宽度,然后除以2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM