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

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

问题描述

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

我尝试了以下方法,但它们并没有影响方向:

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

两个答案camickr &垃圾神提供的工作正常,但不是在我的程序中,我使用我的 JTextArea 作为对象 Message 并将其传递给 OptionPane.

我发现 setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 如果我将它应用到 JOptionPane 内容上将不起作用 .. 这个问题有替代解决方案吗?

类似于我的代码:

import java.awt.*;导入 java.util.*;导入 javax.swing.*;公共类 TextArea 扩展了 JPanel{私人 JTextArea txt = 新 JTextArea();公共文本区域(){设置布局(新的网格布局());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);}私有无效显示(){对象 [] 选项 = {this};JOptionPane 窗格 = 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){新的文本区域().显示();}}

解决方案

滚动条会在左边

scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

<块引用>

所以里面的文字会从右边开始

textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

文本从右侧开始,但在您键入时仍会附加到末尾,而不是插入到行首.

更新:

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

import java.awt.*;导入 java.awt.event.*;导入 java.util.*;导入 javax.swing.*;导入 javax.swing.event.*;公开课测试{public static void main(String args[]) 抛出异常{SwingUtilities.invokeLater(new Runnable() {公共无效运行(){JTextArea textArea = new JTextArea(4, 20);JScrollPane scrollPane = new JScrollPane( textArea );JPanel 面板 = new JPanel();panel.add(scrollPane);scrollPane.addAncestorListener( new AncestorListener(){公共无效祖先添加(AncestorEvent e){JScrollPane scrollPane = (JScrollPane)e.getComponent();scrollPane.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);}公共无效祖先移动(AncestorEvent e){}public voidancestorRemoved(AncestorEvent e) {}});JOptionPane.showMessageDialog(null, panel);}});}}

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

解决方案

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

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

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