如何解决如何从右到左在JOptionPane内部设置JTextArea的方向
| 我里面有JScrollPane
,里面有ѭ1and,并且我试图从右到左设置JTextArea的方向,所以里面的文本将从右开始,滚动条在左边
我已经尝试了以下方法,但是它们并没有影响定向的方向:
txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT);
编辑:
camickr和rashgod提供的两个答案可以正常工作,但在我的程序中无法使用JTextArea作为对象Message并将其传递给OptionPane。
编辑2:
我发现如果在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,options,options[0]);
}
public static void main(String[] args)
{
new TextArea().display();
}
}
解决方法
滚动条将在左侧
scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
所以里面的文字会从右边开始
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
文本从右侧开始,但在您键入时仍会追加到末尾,而不是插入到行首。
更新:
我不知道为什么它在选项窗格中不起作用。这是一个简单的解决方案:
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
, 以下几行解决了我的问题:
jTextArea1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
jTextArea1.setText(<text>);
他们的作用是:
setComponentOrientation()
改变TextArea
的方向;和,
setText()
立即刷新TextArea
,以便正确显示
仅将ComponentOrientation
设置为RIGHT_TO_LEFT
是不够的。 repaint()
不会强制文本重新对齐。对我来说,一种快速的解决方案是更新TextArea的内容。这迫使文本重新调整自身。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。