Eng*_*uad 10 java swing arabic jtextarea
我JScrollPane
跟JTextArea
里面,我试图从右侧设置的JTextArea的方向向左所以里面的文字就会从右边开始,滚动条会出现在左边
我尝试过以下但是它们并没有影响方向的方向:
txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT);
Run Code Online (Sandbox Code Playgroud)
编辑:
两个答案camickr和trashgod提供的工作正常但不在我的程序中我使用我的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, null, options, options[0]);
}
public static void main(String[] args)
{
new TextArea().display();
}
}
Run Code Online (Sandbox Code Playgroud)
滚动条将在左侧
scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Run Code Online (Sandbox Code Playgroud)
所以里面的文字将从右边开始
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Run Code Online (Sandbox Code Playgroud)
文本从右侧开始,但在您键入时仍然会附加到末尾,而不是在行的开头插入.
更新:
我不知道为什么它在选项窗格中不起作用.这是一个简单的解决方案:
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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎有效.
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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22366 次 |
最近记录: |