Java Display Text On Right Side of Text Area

2019-03-02 23:44发布

问题:

The problem I have with ComponentOrientation.RIGHT_TO_LEFT is that characters such as '/' or '!' or '.' are shown on the left side.

I just need text to be drawn from right to left but using standard, western, English left to right notation for text characters.

Is this possible without manually rendering text?

回答1:

I can't generating your issue, can you please use my SSCCE for shown on the left side.

from code

import java.awt.ComponentOrientation;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

public class RightToLeft {

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

            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JTextArea text = new JTextArea(10, 5);
                text.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                text.setText("one/\n "
                        + "!two\n"
                        + ".three\n"
                        + "/four\n"
                        + "five!\n"
                        + "six.\n"
                        + "seven\n"
                        + "eight\n");
                JScrollPane pane = new JScrollPane(text);
                JFrame.setDefaultLookAndFeelDecorated(true);
                JFrame frame = new JFrame("العنوان بالعربي");
                frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                frame.add(pane);
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}


回答2:

Try calling setAlignmentX(Component.RIGHT_ALIGNMENT) for the text field.