如何从右到左设置 JTextArea 的方向(在 JOptionPane 内)
我有 JScrollPane
,其中包含 JTextArea
,我正在尝试从右到左设置 JTextArea 的方向,以便其中的文本将从右侧开始,并且滚动条将打开左边
我尝试了以下方法,但它们没有影响方向的方向:
txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT);
编辑:
两个答案 camickr & rashgod 提供的工作正常,但在我的程序中却不行,我使用 JTextArea 作为对象 Message 并将其传递给 OptionPane。
EDIT2:
我发现如果我将它应用于 JOptionPane 内容,setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
不起作用..这个问题有其他解决方案吗?
类似于我的代码:
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();
}
}
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
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.
EDIT2:
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?
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
。文本从右侧开始,但在您键入时仍会附加到末尾,而不是插入到行首。
更新:
我不知道为什么它在选项窗格中不起作用。这是一个简单的解决方案:
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:
这似乎有效。
This seems to work.
这条线
更改单词的正确顺序。
我有这个结果
this line
change the correct order of the words.
i have this result
以下几行解决了我的问题:
它们的作用是:
setComponentOrientation()
更改TextArea
的方向;并且,setText()
会立即刷新TextArea
,以便其正确显示。仅将
ComponentOrientation
设置为RIGHT_TO_LEFT
本身是不够的。repaint()
不会强制文本自行重新对齐。对我来说,一个快速的解决方案是更新 TextArea 的内容。这迫使文本自行重新调整。The following lines solved my problem:
They serve to:
setComponentOrientation()
changes the orientation of theTextArea
; and,setText()
refreshesTextArea
immediately so it displays properlySimply setting
ComponentOrientation
toRIGHT_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.