如何从右到左设置JTextArea的方向(在JOptionPane内) [英] How to set the orientation of JTextArea from right to left (inside JOptionPane)

查看:131
本文介绍了如何从右到左设置JTextArea的方向(在JOptionPane内)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 JScrollPane ,里面有 JTextArea ,我试图从右到左设置JTextArea的方向,所以里面的文字将从右侧开始,滚动条将在左侧

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

编辑:

两个答案camickr& trashgod提供的工作正常但不在我的程序中我将JTextArea用作对象消息并将其传递给OptionPane。

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:

我发现 setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 如果我将它应用于JOptionPane内容,则不起作用。 。这个问题有另一种解决方案吗?

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?

与我的代码类似:

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


推荐答案


,滚动条将在左侧

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.

更新:

我不知道它为什么不在选项窗格中工作。这是一个简单的解决方案:

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

这篇关于如何从右到左设置JTextArea的方向(在JOptionPane内)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆