如何设置JTextArea的方向从右到左(在JOptionPane中)

我有JScrollPaneJTextArea里面,我想设置JTextArea的方向从右到左,所以其中的文本将从右侧开始,滚动条将在左侧

我已经尝试了以下,但他们并没有影响方向的方向:

 txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT); 

编辑:

两个答案camickr&trashgod提供了很好的工作,但不是在我的程序中使用我的JTextArea作为对象消息并将其传递给OptionPane。

EDIT2:

我想通了setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 如果我将它应用于JOptionPane内容,则不起作用。有没有其他解决scheme来解决这个问题?

类似于我的代码:

 import java.awt.*; import java.util.*; import javax.swing.*; public class TextArea extends JPanel { private JTextArea txt = new JTextArea(); public TextArea() { setLayout(new GridLayout()); txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); JScrollPane scroll = new JScrollPane(txt); scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); setPreferredSize(new Dimension(200,200)); this.add(scroll); } private void display() { Object[] options = {this}; JOptionPane pane = new JOptionPane(); int option = pane.showOptionDialog(null, null, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]); } public static void main(String[] args) { new TextArea().display(); } } 

滚动条会在左边

 scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 

所以它里面的文本将从右侧开始

 textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 

文本从右侧开始,但仍然在input时附加到最后,而不是插入到行首。

更新:

我不知道为什么它不能在选项窗格中工作。 这是一个简单的解决scheme:

 import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; public class Test { public static void main(String args[]) throws Exception { SwingUtilities.invokeLater(new Runnable() { public void run() { JTextArea textArea = new JTextArea(4, 20); JScrollPane scrollPane = new JScrollPane( textArea ); JPanel panel = new JPanel(); panel.add( scrollPane ); scrollPane.addAncestorListener( new AncestorListener() { public void ancestorAdded(AncestorEvent e) { JScrollPane scrollPane = (JScrollPane)e.getComponent(); scrollPane.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); } public void ancestorMoved(AncestorEvent e) {} public void ancestorRemoved(AncestorEvent e) {} }); JOptionPane.showMessageDialog(null, panel); } }); } } 

这似乎工作。

在这里输入图像描述

 import java.awt.ComponentOrientation; import java.awt.EventQueue; import java.awt.GridLayout; import java.util.Locale; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** @see http://stackoverflow.com/questions/6475320 */ public class RTLTextArea extends JPanel { private static final String s = "مرحبا العالم"; private JTextArea jta = new JTextArea(7, 5); private Locale arabic = new Locale("ar", "KW"); private ComponentOrientation arabicOrientation = ComponentOrientation.getOrientation(arabic); public RTLTextArea() { this.setLayout(new GridLayout()); this.add(new JScrollPane(jta)); this.applyComponentOrientation(arabicOrientation); for (int i = 0; i < 8; i++) { jta.append(s + "\n"); } } private void display() { JFrame f = new JFrame("RTLTextAre"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new RTLTextArea().display(); } }); } } 

这条线

setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)

改变单词的正确顺序。

我有这个结果

千字节80.78