java – 如何从右到左设置JTextArea的方向(JOptionPane内)
发布时间:2020-12-14 05:43:25 所属栏目:Java 来源:网络整理
导读:我有JScrollPane与JTextArea里面,我试图从右到左设置JTextArea的方向,所以其中的文本将从右边开始,滚动条将在左边 我尝试了以下方法,但并没有影响方向的方向: txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);txt.setComponentOrientat
|
我有JScrollPane与JTextArea里面,我试图从右到左设置JTextArea的方向,所以其中的文本将从右边开始,滚动条将在左边
我尝试了以下方法,但并没有影响方向的方向: txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT); 编辑: 两个答案camickr&垃圾邮件提供工作正常,但不在我的程序中,我使用我的JTextArea作为一个对象消息,并将其传递给OptionPane. EDIT2: 我想出了setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);如果我将其应用于JOptionPane内容,则不起作用.这个问题有替代的解决方案吗? 类似于我的代码: 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);
}
});
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
