如何居中文本和一个JComponent在JTextPane中垂直?文本和、JComponent、JTextPane

由网友(绾起梨花月)分享简介:目前看起来如此如何做,所以它看起来如此? What to do so that it looks so? 下面是我的code:JFrame f = new JFrame();JTextPane textPane = new JTextPane();JTextField component = new JTextFie...

目前看起来如此

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

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

在一个问题是靠近这个题目,我发现: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

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).

阅读全文

相关推荐

最新文章