如何从右到左设置 JTextArea 的方向(在 JOptionPane 内)

发布于 2024-11-17 06:08:53 字数 1535 浏览 4 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩心不撩汉 2024-11-24 06:08:53

滚动条将位于左侧

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

and the scrollbar will be on the left

scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

so the text inside it will start from the right

textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

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:

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);
            }
        });
    }
}
王权女流氓 2024-11-24 06:08:53

这似乎有效。

在此处输入图像描述

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

This seems to work.

enter image description here

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();
            }
        });
    }
}
撩动你心 2024-11-24 06:08:53

这条线

setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)

更改单词的正确顺序。

我有这个结果

千字节80.78

this line

setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)

change the correct order of the words.

i have this result

KBytes 80.78

笑饮青盏花 2024-11-24 06:08:53

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

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

它们的作用是:

  1. setComponentOrientation() 更改 TextArea 的方向;并且,
  2. setText() 会立即刷新 TextArea,以便其正确显示。

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

The following lines solved my problem:

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

They serve to:

  1. setComponentOrientation() changes the orientation of the TextArea; and,
  2. setText() refreshes TextArea immediately so it displays properly

Simply setting ComponentOrientation to RIGHT_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文