TextAlignment在Java Swing jTextPane中不起作用 [英] Textalignment is not working in java swing jTextPane

查看:69
本文介绍了TextAlignment在Java Swing jTextPane中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to align two sections of texts on the same line: first section should be aligned to the left side, and the other section should be aligned to the right side of the java swing JTextPane. I tried to use style interface and Styleconstants class to align the text, but it didn't work. But when I applied some other styles, such as Styleconstants.setFontSize(), Styleconstants.setForeGroundColor(), on the same text, it's working fine.

这是我的代码:

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

它的输出类似于

nametimeHis

但是我想要这样的输出

名称              b ;              nbsp; b ;        timeHis

name                                                     timeHis

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

(exact opposite side in JTextpane in same line )

我的代码有什么问题吗?我该如何解决这个问题?

Is there anything wrong in my code? How can I solve this problem?

推荐答案

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

I don't think you can set two different alignments for the same line (AFAIK you can't do that even in professional text editors - just by setting alignment). I rewrote your code like this:

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

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

Size and color are OK but alignment for both strings is right. If you swap alignments in the last two lines like this:

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

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

both will be left aligned, but if you add a new line in your left string:

String left = "name\n";

结盟也很荣幸.

the alignment also becomes honored. The setParagraphAttributes method does exactly what it says - the point is that alignment is a paragraph attribute - open OO Writer or MS Word(pad), write some text and align it - it will be clearer.

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

When I want to achieve that alignment in MS Word/OO Writer I make a table with invisible borders, and set the left column the left alignment and to right column the right alignment. That would lead to conclusion that we need the HTMLDocument (because you can make a table in HTML) class which implements the StyledDocument interface. So I tried this:

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

这将导致没有样式.这是有道理的,因为层叠样式表 是设置HTML样式的正确方法.此构造函数对此也进行了解释:

which results in no style. It makes sense since Cascading Style Sheets is the proper way to style HTML. This constructor explains it also:

public HTMLDocument(StyleSheet styles)

这里是 StyleSheet 构造函数参数.

Here's a link to the StyleSheet constructor argument.

但是,这可以解决问题:

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

EditorKit :

建立文本组件所需的一组东西,以使其成为某种类型的文本内容的合理运行的编辑器. EditorKit充当某种策略的工厂.例如,可以提供可替换为其他实现的html和rtf实现.

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方法是:

A few EditorKit methods are:

  • createCaret()
  • getContentType()
  • read(Reader in, Document doc, int pos)
  • write(Writer out, Document doc, int pos, int len)
  • createCaret()
  • getContentType()
  • read(Reader in, Document doc, int pos)
  • write(Writer out, Document doc, int pos, int len)

因此,基本上,它是

so, basically it is a programmable editor (through read and write methods) for a text component's text content, namely the Document:

该文档是文本的容器,用作摆动文本组件的模型.该界面的目标是从非常简单的需求(纯文本文本字段)扩展到复杂的需求(例如HTML或XML文档).

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文本内容的可编程编辑器,即

Hence, HTMLEditorKit is a programmable editor for a text component's HTML text content, namely the HTMLDocument - take a close look at the inheritance chain of this class and the implementing interfaces - you'll notice almost all of them throughout this answer.

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

Now, this is the magic part from the HTMLEditorKit apidoc:

尽管默认情况下文档提供HTML支持,但没有什么可以阻止对非HTML标签的支持,这些非HTML标签会导致元素结构的替换.

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

因此,此类的默认实现似乎翻译了我们的样式属性到HTML/CSS的方式与我们想要的方式相同:同一行中的左右对齐.

So it looks like the default implementation of this class translated our style attributes to HTML/CSS just the way we wanted them: left and right alignment in the same line.

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

It makes sense because with HTML/CSS you can do it in at least two ways:

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

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

and there is most probably more ways to do it.

这篇关于TextAlignment在Java Swing jTextPane中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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