如何居中文本和一个JComponent在JTextPane中垂直? [英] How to center text and a JComponent in a JTextPane vertically?

查看:1276
本文介绍了如何居中文本和一个JComponent在JTextPane中垂直?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前看起来如此

如何做,所以它看起来如此?

What to do so that it looks so?

下面是我的code:

    JFrame f = new JFrame();
    JTextPane textPane = new JTextPane();

    JTextField component = new JTextField("      ");
    component.setMaximumSize(component.getPreferredSize());
    textPane.setSelectionStart(textPane.getDocument().getLength());
    textPane.setSelectionEnd(textPane.getDocument().getLength());
    textPane.insertComponent(component);
    try {
        textPane.getDocument().insertString(textPane.getDocument().getLength(), "text",
            new SimpleAttributeSet());
    } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    f.add(new JScrollPane(textPane));
    f.setSize(200, 100);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

在一个问题是靠近这个题目,我发现:<一href="http://stackoverflow.com/questions/6506712/jtextpane-insert-component-faulty-vertical-alignment">JTextPane插入部件,故障垂直对齐 但没有答案如何改变对齐方式。但必须根据讨论有可能的。

The single question which is near to this topic I found: JTextPane insert component, faulty vertical alignment But there is no answer how to change the alignment. But it must be possible according to the discussion there.

推荐答案

您可以使用此的http:// java-sl.com/tip_center_vertically.html

这应该与 JComponent上为好。

您也可以覆盖 LABELVIEW的 获取preferredSpan()添加一些空间的底部。

You can also override LabelView's getPreferredSpan() adding some space to the bottom.

另外,您可以尝试重写 RowView 的ParagraphView

Alternatively you can try to override RowView inner class in ParagraphView

<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/javax/swing/text/ParagraphView.java#ParagraphView.Row" rel="nofollow">http://grep$c$c.com/file/repository.grep$c$c.com/java/root/jdk/openjdk/6-b14/javax/swing/text/ParagraphView.java#ParagraphView.Row

这点,内部类行扩展BoxView中

That points to inner class Row extends BoxView

您应该用自己的替换。尝试重写

You should replace it with own one. Try to override

public float getAlignment(int axis) 

返回CENTER(0.5)。如果这没有帮助覆盖layoutMinorAxis(0返回适当的偏移(移动)。

to return CENTER (0.5). If this does not help override layoutMinorAxis(0 to return proper offsets (shifted).

这篇关于如何居中文本和一个JComponent在JTextPane中垂直?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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