JPanel TitledBorder中的标题截断-Java swing [英] Title truncation in JPanel TitledBorder - Java swing

查看:169
本文介绍了JPanel TitledBorder中的标题截断-Java swing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有TitledBorder的JPanel,但是面板的内容比边框中的标题窄,并且标题被截断了. 我正在为JPanel使用BoxLayout,如此处所述,请注意手动设置宽度.我试图根据TitledBorder getMinimumSize()函数以及其组件的宽度设置面板的最小,最大和首选宽度,但所有这些都不起作用.唯一可行的方法是使用盒装填充器,但引入了不希望的压痕.

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);
    }

推荐答案

如何计算所需宽度:

       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);
        }

这篇关于JPanel TitledBorder中的标题截断-Java swing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆