Java中JTextArea的自动动态扩展/收缩 [英] automatic dynamic expansion / contraction of JTextArea in Java

查看:287
本文介绍了Java中JTextArea的自动动态扩展/收缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先创建一个特定大小的JTextArea.用户可以在其中添加文本,但是如果文本太长(垂直或水平),它将被截断.我希望JTextArea自动扩展或收缩(用于删除文本).

I start off by creating a JTextArea of a specific size. The user can add text within it but it will get cut off if it becomes too long (vertically or horizontally). I want the JTextArea to automatically expand or contract (for deletion of text).

将来我可能会允许用户更改字体和字体大小,因此,如果我可以避免使事物增加/减少一定的大小,那将是很好的选择.

I may allow users to change font and font size in the future, so it would be good if I could avoid making things increase/decrease by a certain size.

我当前正在使用边界来调整我的JTextArea的大小.也许我应该按行和列调整大小,并使用侦听器并适当执行操作?

I am currently using bounds to size my JTextArea. Perhaps I should size by rows and columns and use a listener and act appropriately?

提前谢谢!

推荐答案

我无法想象您为什么要这样做,为什么不将JTextArea放在JScrollPane中,但是好的,我会继续工作. .也许像这样:

I can't imagine why you'd want to do this, why not put a JTextArea in a JScrollPane, but ok, i'll play along... Maybe something like this:

import java.awt.Container;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;


public class Expander extends JFrame {

    private final JTextArea area;
    private int hSize = 1;
    private int vSize = 1;

    public Expander() {
        Container cp = getContentPane();
        cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));

        cp.add(Box.createHorizontalGlue());
        area = new JTextArea(vSize, hSize);
        cp.add(area);
        cp.add(Box.createHorizontalGlue());

        area.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                adjust();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                adjust();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                adjust();
            }
        });

        pack();
    }

    private void adjust() {
        int maxColumns = getMaxColumns();
        if ((area.getLineCount() != vSize) || (maxColumns != hSize)) {
            hSize = maxColumns;
            vSize = area.getLineCount();
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    area.setColumns(hSize);
                    area.setRows(vSize);
                    Expander.this.doLayout();
                    Expander.this.pack();
                }
            });
        }
    }

    private int getMaxColumns() {
        int startOffset = 0;
        int maxColumns = 0;
        for (int i = 0; i < area.getLineCount(); i++) {
            try {
                int endOffset = area.getLineEndOffset(i);
                int lineSize = endOffset - startOffset;
                if (lineSize > maxColumns) {
                    maxColumns = lineSize;
                }
                startOffset = endOffset;
            } catch (BadLocationException ble) {
            }
        }

        return maxColumns;
    }

    public static void main(String[] args) {

        Expander e = new Expander();
        e.setLocationRelativeTo(null);
        e.setVisible(true);
    }
}

这篇关于Java中JTextArea的自动动态扩展/收缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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