使JTextArea滚动,以便在用户键入更多文本时始终显示插入符位置 [英] Make JTextArea scroll so caret position is always displayed as user types more text

查看:86
本文介绍了使JTextArea滚动,以便在用户键入更多文本时始终显示插入符位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何滚动JTextArea,以便在用户键入更多文本时始终显示插入符号的位置?

How to make JTextArea scroll so caret position is always displayed as user types more text?

对我来说,这似乎是一个愚蠢的问题,我觉得必须先问过这个问题,但我已经搜索过了,却找不到答案.

This seems like a stupid question to me and I feel like it must have been asked before, yet I have searched and cannot find the answer.

我有一个多行文本区域,嵌入在JScrollPane中.如果用户一直键入直到填满文本区域,最终插入符号将变为不可见(在显示的区域下方),并且用户将看不到他正在键入的内容.这将是默认行为,这似乎很奇怪.我需要做些什么来确保文本区域始终滚动到插入符号所在的行.

I have a multiline text area, embedded in a JScrollPane. If the user keeps typing until he fills the text area, eventually the caret becomes invisible (below the area shown) and the user cannot see what he is typing. It seems odd that this would be the default behavior. What do I need to do to make sure the text area always scrolls to the line where the caret is.

我应该提到在文本区域启用了换行.

I should mention that line wrap is enabled in the text area.

推荐答案

在我看来,setXxXSize()方法应与有限数量的组件一起使用,并要记住后面使用的Layout,具体取决于哪个尊重程序员提供的大小调整提示.

In my opinion, setXxXSize() methods are to be used with limited number of components and keeping in mind the Layout used behind, depending on which Layout Manager respects the sizing hints provided by the programmer.

因此,对于像JTextArea这样的组件,其RowsColumns足以足以提示Layout关注点以计算其大小,因此不应将其与硬编码的大小相关联,使用尽量使用某些setXxXSize()方法.

Hence for a component like JTextArea, whose Rows and Columns are sufficient enough, to provide a hint to the Layout concern to calculate its size, should not be associated with a hard coded size, using some setXxXSize() methods, as much as possible.

在这里,我提供了相同的示例代码:

Here I have provided a sample code for the same :

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.EventHandler;
import javax.swing.*;

public class DialogWithScroller
{
    private JFrame frame;
    private JButton showButton;
    private MyDialog myDialog;

    private void displayGUI()
    {
        frame = new JFrame("Dialog ScrollPane Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new BorderLayout(5, 5));
        showButton = new JButton("Show Dialog");
        showButton.addActionListener((ActionListener)
                EventHandler.create(ActionListener.class,
                        DialogWithScroller.this, "buttonActions", ""));     
        contentPane.add(showButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }   

    public void buttonActions(ActionEvent ae)
    {
        myDialog = new MyDialog(frame
                , "TextArea with ScrollPane", true);
    }

    public static void main(String[] args)
    {
        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                new DialogWithScroller().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

class MyDialog extends JDialog
{
    private JTextArea tArea;
    private JButton hideButton;

    private ActionListener buttonActions =
                            new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            MyDialog.this.dispose();
        }
    };

    public MyDialog()
    {}

    public MyDialog(Frame owner, String title, boolean modal)
    {
        super(owner, title, modal);
        displayGUI();
    }

    private void displayGUI()
    {
        JPanel contentPane = new JPanel(
                        new BorderLayout(5, 5));
        contentPane.setBorder(
                BorderFactory.createTitledBorder(
                            "My Personal Text Area"));
        /*
         * Here one can simply initialize the
         * JTextArea like this too, using the
         * constructor itself for specifying
         * the Rows and Columns, which will
         * help the layout concern to determine
         * its size
         */
        tArea = new JTextArea(20, 20);
        tArea.setLineWrap(true);
        tArea.setWrapStyleWord(true);
        JScrollPane textScroller = new JScrollPane(tArea);
        //textScroller.setViewportView(tArea);

        hideButton = new JButton("Hide Dialog");
        hideButton.addActionListener(buttonActions);

        contentPane.add(textScroller, BorderLayout.CENTER);
        contentPane.add(hideButton, BorderLayout.PAGE_END);
        setContentPane(contentPane);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }   
}

调用 Window.pack() 使此Window的大小适合其子组件的首选大小和布局.尽管这样的调用,必须在程序员完成将所有组件添加到容器之后,然后再将容器设置为可见状态.

It is always considered to be a wise practice to call Window.pack(), on the container, which causes this Window to be sized to fit the preferred size and layouts of its subcomponents. Though calls like this, must be made after the programmer is done adding all components to the container and before setting the container to the visible state.

这篇关于使JTextArea滚动,以便在用户键入更多文本时始终显示插入符位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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