ComponentView在顶部有不必要的额外插入

时间:2015-08-07 09:35:32

标签: java swing

我在创建自定义文档时遇到问题,我需要使用ComponentViews(因为我想使用普通的swing组件监听器)。

目前的情况如下: Wrong offset

显然,额外的填充(或者我发现,父亲的BoxView中的minorInset)是错误的,并将整个组件向下移动几个像素,确切地说是6。

我按如下方式添加组件:

public class InlineLabelTest extends JFrame {

    public InlineLabelTest() {
        try {
            final JTextPane textPane = new JTextPane();
            textPane.setEditorKit(new InlineLabelKit());

            final LabelDocument document = (LabelDocument) textPane.getDocument();
            document.insertString(0, "Lorem ipsum", new SimpleAttributeSet());
            document.insertLabel(document.getLength(), "dateAndTime");

            setLayout(new BorderLayout());
            add(new JScrollPane(textPane));

        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new InlineLabelTest().setVisible(true));
    }


    private static class InlineLabelKit extends StyledEditorKit {
        private LabelViewFactory factory;


        @Override
        public ViewFactory getViewFactory() {
            if (factory == null) {
                factory = new LabelViewFactory();
            }

            return factory;
        }

        @Override
        public Document createDefaultDocument() {
            return new LabelDocument();
        }

    }

    private static class LabelDocument extends DefaultStyledDocument {

        public void insertLabel(int offset, String label) {
            final ArrayList<ElementSpec> specs = new ArrayList<>();
            final JButton flag = new JButton(label);

            // The combination of the border and the font adds the inset
            flag.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
            flag.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));

            final SimpleAttributeSet inner = new SimpleAttributeSet();
            StyleConstants.setComponent(inner, flag);

            specs.add(new ElementSpec(inner, ElementSpec.StartTagType));
            specs.add(new ElementSpec(inner, ElementSpec.ContentType, label.toCharArray(), 0, label.length()));
            specs.add(new ElementSpec(inner, ElementSpec.EndTagType));

            try {
                insert(offset, specs.toArray(new ElementSpec[specs.size()]));

            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        }
    }

    private static class LabelViewFactory implements ViewFactory {

        @Override
        public View create(Element elem) {
            switch (elem.getName()) {
                case AbstractDocument.SectionElementName: return new BoxView(elem, View.Y_AXIS);
                case AbstractDocument.ParagraphElementName: return new ParagraphView(elem);
                case StyleConstants.ComponentElementName: return new ComponentView(elem);
                case StyleConstants.IconElementName: return new IconView(elem);
                default: return new LabelView(elem);
            }
        }

    }

}

似乎自定义边框和字体的组合(或任何影响其默认尺寸的更改)会创建插图。

如何摆脱额外的填充?为什么系统甚至在ComponentView周围添加BoxView?我错过了什么吗?

提前多多谢谢!

1 个答案:

答案 0 :(得分:1)

好的,经过一整天的研究和调试后,我发现我手动必须在我想要设置的组件上设置Y对齐。

实际发生的事情(简单来说)是,文本基线大约是文本高度的85%,而组件基线默认为50%(因为它不是文本,而是一个框),因此添加了从顶部插入的差异为35%。

通过在要添加的组件上指定Y对齐,可以轻松修复此问题:

JButton component = new JButton(text);
FontMetrics fontMetrics = component.getFontMetrics(component.getFont());
LineMetrics metrics = fontMetrics.getLineMetrics(text, component.getGraphics());

float ascent = metrics.getAscent(), descent = metrics.getDescent();
component.setAlignmentY(ascent / (ascent + descent));