TitledBorder 中的标题折叠

发布于 2024-11-02 10:08:23 字数 253 浏览 4 评论 0原文

我有一个 TitledBorder ,其标题大于正文。 它被削减,长度根据身体的最大长度设定。

假设我在正文中有一个标签,上面写着 TEST ,并且它被标题为 TESTING TITLE 的 TitledBorder 括起来。 在本例中,标题被缩短并显示为 TESTI

我正在使用 MigLayout 作为正文。

有什么方法可以显示完整的标题,无论其包含的内容如何?

I've a TitledBorder having title larger than the body.
Its get cut down and the length sets as per the maximum length of the body.

Suppose I have a label in the body saying TEST and its enclosed by TitledBorder having title TESTING TITLE.
In this case the title is cut down and displayed as TESTI

I'm using MigLayout for the body.

Any way to the show the full title irrespective to the content it contains?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙女山的月亮 2024-11-09 10:08:23

Border 不直接参与 LayoutManager 的大小评估(它们本身不是 JComponent ,而只是 JComponent 周围的装饰)。

因此,确保 TitledBorder 中的标题完全可见的唯一方法是为其分配的面板(或者更确切地说是嵌入在该面板中的组件)确保最小尺寸。

在这种情况下,最小大小的计算应基于 TitledBorder 的当前标题以及 TitledBorder 用于绘制其标题的 Font

说实话,我不确定,所有这些作品是否值得写。

根据 TitledBorder 中包含的确切组件,更简单的解决方案是使用 JTextField.setColumns(n),其中 n 与标题相比足够大。

实际上,更好的方法是完全避免使用 TitledBorder,正如来自 JGoodies 名声,并将其替换为彩色 JLabel 和水平 JSeparator,它将完全参与布局,因此可用于尺寸计算。

Borders don't directly participate in size evaluation by the LayoutManager (they are not a JComponent themselves but just decorations around a JComponent).

Hence the only way to ensure that the title in a TitledBorder is completely visible is to enfore a minimum size for the panel it is assigned (or rather for components embedded in that panel).

Minimum size calculation should in this case be based on the current title of your TitledBorder and the Font used by TitledBorder to paint its title.

To be honest, I am not sure, all this work is worth writing.

Based on the exact components comntained inside your TitledBorder, a simpler solution would be to use JTextField.setColumns(n) where n is big enough compared with the title.

Actually a better way would be to avoid TitledBorder altogether, as suggested by Karsten Lentszch from the JGoodies fame, and replace it with a colored JLabel and a horizontal JSeparator, that would fully participate in the layout and hence be used for size computation.

时光清浅 2024-11-09 10:08:23

我发现添加适当宽度的水平支柱将强制面板调整大小,以便显示整个标题:

    myPanel.add(Box.createHorizontalStrut(desiredWidth));

我确信可能有一种方法可以在运行时动态找到所需的宽度,但我只是这样做懒惰的方法:将其设置为覆盖我的 TitledBorder 标题文本大小的宽度。

I have found that adding a Horizontal Strut of an appropriate width will force the panel to resize so that it shows the entire title:

    myPanel.add(Box.createHorizontalStrut(desiredWidth));

I'm sure there is probably a way to dynamically find the desired width at runtime, but I just do it the lazy way: set it to a width that covers the size of my TitledBorder title text.

弱骨蛰伏 2024-11-09 10:08:23

这对我来说效果很好:

 final String myPanelTitle = UIManager.getString("myPanel_title");
 final Border myPanelBorder = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GREEN), myPanelTitle);
 JPanel myPanel = new JPanel(){
      @Override
      public Dimension getPreferredSize(){
           Dimension d = super.getPreferredSize();
           int insets = 2 * (myPanelBorder.getBorderInsets(this).left + myPanelBorder.getBorderInsets(this).right);
           double minWidth = this.getFontMetrics(this.getFont()).stringWidth(myPanelTitle) + insets;
           if(d.getWidth() > minWidth)
                 minWidth = d.getWidth();
           return new Dimension((int)minWidth, d.height);
       }
 };
 myPanel.setBorder(myPanelBorder);

This works well for me:

 final String myPanelTitle = UIManager.getString("myPanel_title");
 final Border myPanelBorder = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GREEN), myPanelTitle);
 JPanel myPanel = new JPanel(){
      @Override
      public Dimension getPreferredSize(){
           Dimension d = super.getPreferredSize();
           int insets = 2 * (myPanelBorder.getBorderInsets(this).left + myPanelBorder.getBorderInsets(this).right);
           double minWidth = this.getFontMetrics(this.getFont()).stringWidth(myPanelTitle) + insets;
           if(d.getWidth() > minWidth)
                 minWidth = d.getWidth();
           return new Dimension((int)minWidth, d.height);
       }
 };
 myPanel.setBorder(myPanelBorder);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文