如何在TitledBorder

时间:2018-07-20 13:39:45

标签: java swing titled-border

我已经尝试过getTitlePosition,但是它似乎只返回边框的起点0,而不是标题本身。我也尝试过getBorderJustification,但是它返回了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);

1 个答案:

答案 0 :(得分:2)

  

如果标题居中,我不知道如何确保弹出窗口保持在标题上方

因此,您需要检查标题位置。如果居中,则使用基本数学:

  1. 您知道面板的宽度
  2. 您知道弹出窗口的宽度
  3. 从面板宽度中减去弹出窗口的宽度,然后除以2。