Jtextpane 中每一行的不同方向

Different Orientation for each line in Jtextpane

我想在 JTextPane 中附加文本,我让每一行都有不同的颜色,但是当我尝试从左到右和从右到左制作行时,它不起作用

这是我的方法代码

private void appendToPane(JTextPane tp, String msg, Color c , String rientation)
{
        ComponentOrientation ornt = ComponentOrientation.LEFT_TO_RIGHT;

        if(orientation.equals("left"))
            ornt = ComponentOrientation.LEFT_TO_RIGHT;


        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

        AttributeSet or     =  sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Orientation , StyleConstants.ALIGN_RIGHT );
        AttributeSet fnt    = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Background , Color.YELLOW);
        AttributeSet fontSize = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.FontSize, 25);

         // aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
         // aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

        int len = tp.getDocument().getLength();
        tp.setCaretPosition(len);
        tp.setCharacterAttributes(aset, false);
        tp.setCharacterAttributes(or , false);
        tp.setCharacterAttributes(fnt, false);
        tp.setCharacterAttributes(fontSize, false);


//      if(orientation.equals("left"))
//          tp.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
//      else
//          tp.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

//      if(orientation != null)
//          tp.setAlignmentX(Component.RIGHT_ALIGNMENT);
//      else
//          tp.setAlignmentX(orientation);

//      tp.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        tp.replaceSelection(msg);
    }

因此,如果您的目标是更改对齐方式,这里是您的代码示例:

package test;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("test");

                JTextPane testPane = new JTextPane();
                testPane.setText("left\nright");

                StyledDocument doc = testPane.getStyledDocument();

                //left alignment
                SimpleAttributeSet myAttributeSet = new SimpleAttributeSet();
                StyleConstants.setAlignment(myAttributeSet, StyleConstants.ALIGN_LEFT);
                doc.setParagraphAttributes(0, 4, myAttributeSet, false);

                //right alignment
                StyleConstants.setAlignment(myAttributeSet, StyleConstants.ALIGN_RIGHT);
                doc.setParagraphAttributes(5, 9, myAttributeSet, false);

                frame.getContentPane().setLayout(new BorderLayout());
                frame.getContentPane().add(testPane,BorderLayout.CENTER);
                frame.setSize(200,200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

如果您的目标是更改文本的方向,请选中 this about Bidirectional text

如果您的目标只是从右向左书写文本,您只需在文档中添加一个 DocumentFilter 并从那里反转文档的逻辑(向后将变为向前,插入符号将是在插入之前而不是之后移动,插入的文本将被镜像等)

如果您能更清楚地展示您的预期结果,我们可能会为您提供更多帮助。