我在创建需要ComponentViews的自定义文档时遇到了麻烦(因为我想使用常规的swing组件侦听器)。
当前是这样的:
显然,额外的填充(或者我发现,父级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?我有什么想念的吗?
提前非常感谢!
最佳答案
好的,经过一整天的研究和调试,我发现必须手动在要设置的组件上设置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));