【问题标题】:How to set the orientation of JTextArea from right to left (inside JOptionPane)如何设置 JTextArea 的方向从右到左(在 JOptionPane 内)
【发布时间】:2011-09-22 10:49:00
【问题描述】:

我有JScrollPane,里面有JTextArea,我试图将JTextArea的方向设置为从右到左,这样它里面的文本将从右边开始,滚动条在左边

我尝试了以下方法,但它们不影响方向的方向:

txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT);

编辑:

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();
    }
}

【问题讨论】:

  • 最后,一个SSCCE,应该随每个问题发布。查看我的更新。

标签: java swing arabic jtextarea


【解决方案1】:

滚动条在左边

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);
            }
        });
    }
}

【讨论】:

  • 对我来说很好,发布您的 SSCCE!
  • @camickr 从哪里可以看到我的 SSCCE?
  • 这也有效,但不适用于将 JTextArea 作为对象消息传递给 OptionPane 的程序
  • +1 有趣;看起来AncestorListenerapplyComponentOrientation() 相得益彰。
【解决方案2】:

这似乎行得通。

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();
            }
        });
    }
}

【讨论】:

  • 这很好用,但我不知道为什么它在我的程序中不起作用:/我必须再次重新检查我的代码
  • 这就是为什么要求您发布您的 SSCCE 的原因!不要指望我们编写代码,我已经告诉过你它工作得很好。当您所要做的就是尝试一行代码时,我不相信勺子喂食代码。那样你什么都学不到。
  • 更新了使用applyComponentOrientation()的示例。
【解决方案3】:

这一行

setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)

改变单词的正确顺序。

我有这个结果

千字节 80.78

【讨论】:

    【解决方案4】:

    以下几行解决了我的问题:

    jTextArea1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    jTextArea1.setText(<text>);
    

    它们的作用是:

    1. setComponentOrientation() 改变TextArea 的方向;并且,
    2. setText() 立即刷新 TextArea 使其正确显示

    仅将ComponentOrientation 设置为RIGHT_TO_LEFT 本身是不够的。 repaint() 不会强制文本重新对齐。对我来说,一个快速的解决方案是更新 TextArea 的内容。这迫使文本重新调整自身。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      相关资源
      最近更新 更多