+-
TextAlignment在Java Swing jTextPane中不起作用
我想在同一行上对齐文本的两个部分:第一部分应与 java swing JTextPane的左侧对齐,而另一部分应与java swing JTextPane的右侧对齐.我试图使用样式界面和Styleconstants类来对齐文本,但是没有用.但是,当我在同一文本上应用其他样式(例如Styleconstants.setFontSize(),Styleconstants.setForeGroundColor())时,效果很好.

这是我的代码:

JTextPane pane = new JTextPane();
StyledDocument sdoc = pane.getStyledDocument();
SimpleAttributeSet rightAlign = new SimpleAttributeSet();
StyleConstants.setAlignment(rightAlign, StyleConstants.ALIGN_RIGHT);
StyleConstants.setForeground(rightAlign, Color.lightGray);
StyleConstants.setFontSize(rightAlign, 11);
sdoc.insertString(sdoc.getLength(), "name", null);
sdoc.insertString(sdoc.getLength(), "timeHis" + "\n", rightAlign);

它给出的输出像

他的名字

但我想要这样的输出

姓名姓名timeHis

(在同一行的JTextpane中恰好相反)

我的代码有什么问题吗?我怎么解决这个问题?

最佳答案
我认为您不能为同一行设置两个不同的对齐方式(AFAIK即使在专业的文本编辑器中也无法做到这一点-仅通过设置对齐方式即可).我这样重写了您的代码:

    SimpleAttributeSet rightAlign = new SimpleAttributeSet();
    SimpleAttributeSet leftAlign = new SimpleAttributeSet();

    StyleConstants.setAlignment(rightAlign, StyleConstants.ALIGN_RIGHT);
    StyleConstants.setForeground(rightAlign, Color.lightGray);
    StyleConstants.setFontSize(rightAlign, 11);

    StyleConstants.setAlignment(leftAlign, StyleConstants.ALIGN_LEFT);
    StyleConstants.setForeground(leftAlign, Color.black);
    StyleConstants.setFontSize(leftAlign, 13);

    String left = "name";
    String right = "timeHis\n";

    sdoc.insertString(0, left, leftAlign);
    sdoc.insertString(left.length(), right, rightAlign);

    sdoc.setParagraphAttributes(0, left.length(), leftAlign, false);               
    sdoc.setParagraphAttributes(left.length()+1, sdoc.getLength()-1-left.length(), rightAlign, false);      

大小和颜色都可以,但是两个字符串的对齐都是正确的.如果您在最后两行中交换对齐方式是这样的:

    sdoc.setParagraphAttributes(0, left.length(),  rightAlign, false);               
    sdoc.setParagraphAttributes(left.length()+1, sdoc.getLength()-1-left.length(),leftAlign, false);  

两者都将保持左对齐,但是如果您在左字符串中添加新行:

String left = "name\n";

一致也很荣幸. setParagraphAttributes方法完全按照它说的去做-关键是对齐方式是paragraph属性-打开OO Writer或MS Word(pad),写一些文本并对齐它-会更清楚.

当我想在MS Word / OO Writer中实现对齐时,我制作了一个带有不可见边框的表格,并将左列设置为左对齐,将右列设置为右对齐.
这将得出结论,我们需要实现StyledDocument interface的HTMLDocument(因为您可以在HTML中创建table)类.因此,我尝试了以下操作:

JTextPane pane = new JTextPane(new HTMLDocument());

结果没有风格.这很有意义,因为Cascading Style Sheets是样式HTML的正确方法. constructor也对此进行了说明:

public HTMLDocument(StyleSheet styles)

这是指向StyleSheet构造函数参数的链接.

但是,这可以解决问题:

JTextPane pane = new JTextPane();
pane.setEditorKit(new HTMLEditorKit());

An EditorKit:

Establishes the set of things needed by a text component to be a reasonably functioning editor for some type of text content. The EditorKit acts as a factory for some kind of policy. For example, an implementation of html and rtf can be provided that is replaceable with other implementations.

一些EditorKit方法是:

> createCaret()
> getContentType()
>阅读(阅读器输入,文档doc,int pos)
>写(写出,文档doc,int pos,int len)

因此,基本上,它是text component的文本内容(即Document)的可编程编辑器(通过读写方法):

The Document is a container for text that serves as the model for swing text components. The goal for this interface is to scale from very simple needs (a plain text textfield) to complex needs (an HTML or XML document, for example).

因此,HTMLEditorKit是文本组件的HTML文本内容(即HTMLDocument)的可编程编辑器-仔细研究此类的继承链和实现接口-在整个答案中,您几乎都会注意到它们.

现在,这是HTMLEditorKit apidoc的神奇部分:

Although the Document provides HTML support by default, there is nothing preventing support of non-HTML tags that result in alternative element structures.

因此,此类的默认实现似乎将我们的style attributes转换为HTML / CSS的方式恰好是我们希望的:同一行中的左右对齐.

这是有道理的,因为使用HTML / CSS,您至少可以通过两种方式做到这一点:

>制作两列表格并分别设置其对齐方式
>制作两个浮动div并分别设置其对齐方式

而且很可能有更多的方法可以做到这一点.

点击查看更多相关文章

转载注明原文:TextAlignment在Java Swing jTextPane中不起作用 - 乐贴网