Java 如何从右到左设置 JTextArea 的方向(在 JOptionPane 内)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6475320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the orientation of JTextArea from right to left (inside JOptionPane)
提问by Eng.Fouad
I have JScrollPane
with JTextArea
inside it and I am trying to set the JTextArea's orientation from right to left so the text inside it will start from the right and the scrollbar will be on the left
我JScrollPane
跟JTextArea
里面,我想设置从右边的JTextArea的方向向左所以它里面的文本会从右边开始,滚动条会出现在左边
I've tried the following but they didn't affect the direction of the orientation:
我已经尝试了以下但它们并没有影响方向的方向:
txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT);
EDIT:
编辑:
the two answers camickr & trashgod provided work fine but not in my program where I use my JTextArea as an object Message and pass it to OptionPane.
camickr 和trashgod 提供的两个答案工作正常,但在我使用JTextArea 作为对象Message 并将其传递给OptionPane 的程序中没有。
EDIT2:
编辑2:
I figured out that setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
doesn't work if I apply it on the JOptionPane contents .. is there an alternative solution to this issue?
我发现setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
如果我将它应用到 JOptionPane 内容上是行不通的……这个问题有替代解决方案吗?
Similar to my code:
类似于我的代码:
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();
}
}
采纳答案by camickr
and the scrollbar will be on the left
滚动条将在左侧
scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
so the text inside it will start from the right
所以里面的文字会从右边开始
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
The text starts on the right side, but still gets append to the end as you type instead of being inserted at the beginning of the line.
文本从右侧开始,但在您键入时仍会附加到末尾,而不是插入到行首。
Update:
更新:
I don't know why it doesn't work in an option pane. Here is a simple solution:
我不知道为什么它在选项窗格中不起作用。这是一个简单的解决方案:
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);
}
});
}
}
回答by trashgod
This seems to work.
这似乎有效。
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();
}
});
}
}
回答by jhonnyjuncal
this line
这条线
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)
change the correct order of the words.
改变单词的正确顺序。
i have this result
我有这个结果
KBytes 80.78
千字节 80.78
回答by Eric D
The following lines solved my problem:
以下几行解决了我的问题:
jTextArea1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
jTextArea1.setText(<text>);
They serve to:
它们用于:
setComponentOrientation()
changes the orientation of theTextArea
; and,setText()
refreshesTextArea
immediately so it displays properly
setComponentOrientation()
改变 的方向TextArea
;和,setText()
TextArea
立即刷新,以便正确显示
Simply setting ComponentOrientation
to RIGHT_TO_LEFT
is not sufficient by itself. repaint()
doesn't force the text to realign itself. A quick solution for me was to update the contents of the TextArea. That forced the text to realign itself.
仅仅设置ComponentOrientation
为RIGHT_TO_LEFT
本身是不够的。repaint()
不会强制文本重新对齐。对我来说,一个快速的解决方案是更新 TextArea 的内容。这迫使文本重新调整自身。