【问题标题】:How to center text and a JComponent in a JTextPane vertically?如何在 JTextPane 中垂直居中文本和 JComponent?
【发布时间】:2012-12-05 16:25:58
【问题描述】:

目前看起来如此

要怎么做才能让它看起来如此呢?

下面是我的代码:

    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 insert component, faulty vertical alignment 但是没有答案如何更改对齐方式。但根据那里的讨论,它必须是可能的。

【问题讨论】:

  • @DavidKroukamp 我认为垂直对齐会影响垂直定位。第二张图片的文字位置低于第一张图片的文字。

标签: java swing alignment jtextpane jcomponent


【解决方案1】:

你可以用这个http://java-sl.com/tip_center_vertically.html

它也应该与JComponents 一起使用。

您还可以覆盖LabelView's getPreferredSpan() 在底部添加一些空间。

或者,您可以尝试覆盖ParagraphView 中的RowView 内部类

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/text/ParagraphView.java#ParagraphView.Row

指向内部类 Row extends BoxView

你应该用自己的替换它。尝试覆盖

public float getAlignment(int axis) 

返回 CENTER (0.5)。如果这不能帮助覆盖 layoutMinorAxis(0 以返回正确的偏移量(移位)。

【讨论】:

  • getPreferedSpan() 完成了这项工作。我可以用恒定值调整文本位置。但是如果使用不同大小的组件呢?我希望一切看起来都像一串肉。虽然肉块可以有不同的大小,但它们都穿过它们的中心。串是文本。这可以用“RowView”和“ParagraphView”来完成吗?如果是这样,我无法理解如何覆盖一个类,并且 ParagraphView 中也没有这样的内部类。
  • 谢谢!覆盖 getAlignment() 有帮助。
  • @StanislavL 谢谢CenteredBoxView,+1!不过有一个问题,不应该是CenteredBoxView.layoutMajorAxis() 中的textBlockHeight += spans[i]; 而不是textBlockHeight = spans[i];?
【解决方案2】:

使用 JLabel 为您的文档定义样式并在其上设置垂直对齐方式:

Style s = doc.addStyle("icUf", regular);        
ImageIcon icUf = createImageIcon("uf.png", "Unidad Funcional");
if (icUf != null) {
    JLabel jl = new JLabel(icUf);
    jl.setVerticalAlignment(JLabel.CENTER);
    StyleConstants.setComponent(s, jl);
}

插入标签:

doc.insertString(doc.getLength(), " ", doc.getStyle("icUf"));

和正文:

doc.insertString(doc.getLength(), " text ", doc.getStyle("bold"));

【讨论】:

    【解决方案3】:

    根据上面的答案(这对我不起作用,但帮助我找到了这个),我使用了:

    Style s = doc.addStyle("icUf", regular);        
    ImageIcon icUf = createImageIcon("uf.png", "Unidad Funcional");
    if (icUf != null) {
        // create label with icon AND text
        JLabel jl = new JLabel("some text",icUf, SwingConstants.LEFT);
        StyleConstants.setComponent(s, jl);
    }
    doc.insertString(doc.getLength(), " ", doc.getStyle("icUf"))
    

    这正确对齐了文本“一些文本”和图标。

    【讨论】:

      猜你喜欢
      • 2013-10-15
      • 1970-01-01
      • 2021-09-14
      • 2012-11-13
      • 2017-04-05
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 2015-09-16
      相关资源
      最近更新 更多