JPanel TitledBorder 中的标题截断 - Java swing

发布于 2024-11-16 03:12:11 字数 1454 浏览 3 评论 0原文

我有一个带有 TitledBorder 的 JPanel,但面板的内容比边框中的标题窄,并且标题被截断。 我正在将 BoxLayout 用于 JPanel,如此处所示,请注意手动设置宽度。我尝试根据 TitledBorder getMinimumSize() 函数及其组件的宽度设置面板的最小、最大和首选宽度,但都不起作用。唯一有效的方法是使用盒子填充物,但这会产生不需要的压痕。

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

this.jpCases.setLayout(new javax.swing.BoxLayout(this.jpCases, javax.swing.BoxLayout.LINE_AXIS));
        List<Category> categories = db.getCategories();
        for (Category cat : categories) {
            JPanel jp = new JPanel();
            TitledBorder tb = BorderFactory.createTitledBorder(cat.getDescription());

            jp.setBorder(tb);
            jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
            jp.setAlignmentY(Component.TOP_ALIGNMENT);
            jp.setAlignmentX(Component.LEFT_ALIGNMENT);

            List<Case> cases = db.getCasesByCategoryId(cat.getId());
            for (Case c : cases) {
                JRadioButton jrbCase = new JRadioButton();
                jrbCase.setText(c.getDescription());
                jrbCase.setToolTipText(c.getText());
                jrbCase.setMaximumSize(tb.getMinimumSize(jp));
                bgCases.add(jrbCase);
                jp.add(jrbCase);
            }
        //jp.add(new Box.Filler(tb.getMinimumSize(jp), tb.getMinimumSize(jp), tb.getMinimumSize(jp)));
            this.jpCases.add(jp);
    }

I've got a JPanel with a TitledBorder, but the contents of the panel are narrower than the title in the border and the title gets truncated.
I am using BoxLayout for the JPanel which as depicted here pays attention to manual setting of width. I tried to set the minimum, maximum and preferred width of the panel according to the TitledBorder getMinimumSize() function along with the width of its components but all don't work. The only thing that worked was using a box filler but that introduced an undesired indentation.

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

this.jpCases.setLayout(new javax.swing.BoxLayout(this.jpCases, javax.swing.BoxLayout.LINE_AXIS));
        List<Category> categories = db.getCategories();
        for (Category cat : categories) {
            JPanel jp = new JPanel();
            TitledBorder tb = BorderFactory.createTitledBorder(cat.getDescription());

            jp.setBorder(tb);
            jp.setLayout(new BoxLayout(jp, BoxLayout.Y_AXIS));
            jp.setAlignmentY(Component.TOP_ALIGNMENT);
            jp.setAlignmentX(Component.LEFT_ALIGNMENT);

            List<Case> cases = db.getCasesByCategoryId(cat.getId());
            for (Case c : cases) {
                JRadioButton jrbCase = new JRadioButton();
                jrbCase.setText(c.getDescription());
                jrbCase.setToolTipText(c.getText());
                jrbCase.setMaximumSize(tb.getMinimumSize(jp));
                bgCases.add(jrbCase);
                jp.add(jrbCase);
            }
        //jp.add(new Box.Filler(tb.getMinimumSize(jp), tb.getMinimumSize(jp), tb.getMinimumSize(jp)));
            this.jpCases.add(jp);
    }

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

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

发布评论

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

评论(3

木格 2024-11-23 03:12:11

计算所需的宽度怎么样:

       JRadioButton jrb = new JRadioButton();
       int width = (int) SwingUtilities2.getFontMetrics( jrb, jrb.getFont() ).getStringBounds( cat.getDescription(), null ).getWidth();
       for (Case c : cases) {
            JRadioButton jrbCase = new JRadioButton();
            jrbCase.setText(c.getDescription());
            jrbCase.setToolTipText(c.getText());
            jrbCase.setPreferredSize( new Dimension( width, jrbCase.getPreferredSize().height ) );
            bgCases.add(jrbCase);
            jp.add(jrbCase);
        }

What about calculating the needed width:

       JRadioButton jrb = new JRadioButton();
       int width = (int) SwingUtilities2.getFontMetrics( jrb, jrb.getFont() ).getStringBounds( cat.getDescription(), null ).getWidth();
       for (Case c : cases) {
            JRadioButton jrbCase = new JRadioButton();
            jrbCase.setText(c.getDescription());
            jrbCase.setToolTipText(c.getText());
            jrbCase.setPreferredSize( new Dimension( width, jrbCase.getPreferredSize().height ) );
            bgCases.add(jrbCase);
            jp.add(jrbCase);
        }
独闯女儿国 2024-11-23 03:12:11

这对我来说效果很好:

 JPanel aPanel = new JPanel(){
      @Override
      public Dimension getPreferredSize(){
           Dimension d = super.getPreferredSize();
           Border b = this.getBorder();
           if(b == null) return d;
           if(!(b instanceof TitledBorder)) return d;
           TitledBorder tb = (TitledBorder)b;
           if(tb.getTitle() == null) return d;
           int insets = 2 * (tb.getBorderInsets(this).left + tb.getBorderInsets(this).right);
           double minWidth = this.getFontMetrics(tb.getTitleFont()).stringWidth(tb.getTitle()) + insets;
           if(d.getWidth() > minWidth) return d;
           return new Dimension((int)minWidth, d.height);
      }
 };

This Works well for me:

 JPanel aPanel = new JPanel(){
      @Override
      public Dimension getPreferredSize(){
           Dimension d = super.getPreferredSize();
           Border b = this.getBorder();
           if(b == null) return d;
           if(!(b instanceof TitledBorder)) return d;
           TitledBorder tb = (TitledBorder)b;
           if(tb.getTitle() == null) return d;
           int insets = 2 * (tb.getBorderInsets(this).left + tb.getBorderInsets(this).right);
           double minWidth = this.getFontMetrics(tb.getTitleFont()).stringWidth(tb.getTitle()) + insets;
           if(d.getWidth() > minWidth) return d;
           return new Dimension((int)minWidth, d.height);
      }
 };
鹤舞 2024-11-23 03:12:11

我通过在面板的顶部(或底部)添加一个标签(BoxLayout.Y_AXIS)来“解决”它。标签仅包含导致标题宽度相同(或更大)的空格。 (使具有与标题完全相同的文本的标签不可见并不能解决问题。)

I "solved" it by adding a label (BoxLayout.Y_AXIS) to the top (or bottom) of the panel. The label contains only spaces leading to the same (or greater) width of the title. (Making the label with the very same text as the title invisible does not solve it.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文