当 JTextArea 超过一定行数时启用滚动条

发布于 2025-01-08 01:09:07 字数 992 浏览 4 评论 0原文

这是我第一次使用 StackExchange 网站,所以让我们看看效果如何。

所以我一直在用 Java 制作一个“本地”聊天程序,并使用 JTextField 进行聊天输入。但我想允许多线聊天,所以我切换到 JTextArea。我正在使用 GroupLayout(使用 Window Builder Pro - eclipse 构建)来轻松调整窗口/组件的大小。这是一张图片:

在此处输入图像描述

JTabbedPane、JTextArea 和 Send 按钮都包含在 JPanel 中,并且所有左边的东西在它自己的 JPanel 中。因此,我将 JTextArea 和按钮停靠在右侧 JPanel 的底部。 JTextArea 可以垂直调整大小,但按钮不行。 当我输入新行时,我能够使 JTextArea 垂直增长,如下所示:

在此处输入图像描述

但我'我无法想出一种方法,以便如果我在 JTextArea 中输入一定数量的行,则会出现滚动条并防止 JTextArea 占用更多空间。 因此,我尝试将 JTextArea 包装在 JScrollPane 中,但最初禁用滚动条,然后在需要 JTextArea 开始滚动时启用它们,但我了解到,如果我包装它,JScrollPane 不会增长,但仍会像使用滚动条可见,但......没有它们。 :/

** 我想在这里放一个链接,但 StackOverflow 不喜欢我;)

所以,我有点卡住了...... 有什么东西可以做到这一点吗? 我想我可以创建两个不同的 GroupLayout 对象,一个的滚动窗格甚至无效,另一个的滚动窗格有效但停留在一定的大小。在 keyPress 监听器上,我可以检查文本区域是否超过特定限制,然后它会切换面板的布局?内部 JTextArea 仍然是相同的对象,但只是不同的布局对象。对这种方法有何看法?

无论如何,提前感谢所有花时间回答这个问题的人。 :)

this is my first time using any StackExchange website, so let's see how it goes.

So I've been making a 'local' chat program in Java, and was using a JTextField for chat input. But I wanted to allow for multiline chatting so I switched to JTextArea. I'm using a GroupLayout (built with Window Builder Pro - eclipse) for easy window/component resizing. Here's a picture:

enter image description here

The JTabbedPane, the JTextArea and the Send button are all contained in a JPanel, and all the stuff to the left is in it's own JPanel. So I have the JTextArea and the button docked to the bottom of the right JPanel. The JTextArea is allowed to resize vertically, but the button isn't.
I was able to get the JTextArea to grow vertically when I enter new lines, show below:

enter image description here

But I'm unable to think up a way so that if I enter a certain amount of lines into the JTextArea, scrollbars will appear and prevent the JTextArea from taking up any more space.
So I tried wrapping the JTextArea in a JScrollPane but disable to scrollbars initially and then enable them when I needed the JTextArea to start scrolling, but I learned that if I wrap it, the JScrollPane won't grow but will still act like it would with the scrollbars visible but... without them. :/

** I wanted to put a link here, but StackOverflow doesn't like me ;)

So, I'm kind of stuck...
Is there something that does this that I'm missing?
I was thinking that I could just create two different GroupLayout objects, one with the scrollpane not even valid, and then other with the scrollpane valid but stuck at a certain size. On the keyPress listener I could check if the text area exceeds a certain limit, and then it would switch the layout for the panel? The inner JTextArea would still be the same object, but just different layout objects. Opinions on that approach?

Anyway, thanks in advance to all who take their time to answer this. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

挥剑断情 2025-01-15 01:09:07

我编写了一个小程序,仅使用 Swing 控件将 JTextArea 的大小调整为最多 4 行 以下

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class ResizeTextArea {

    public static final int CHAT_ROW_LIMIT = 4;

    public static void main(String[] args) {
        JPanel topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(200, 200));
        topPanel.setBackground(Color.WHITE);

        final JTextArea chatArea = new JTextArea();
        final JScrollPane scrollPane = new JScrollPane(chatArea);

        final JPanel mainPanel = new JPanel(new BorderLayout(5,5));
        mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        mainPanel.add(topPanel, BorderLayout.CENTER);
        mainPanel.add(scrollPane, BorderLayout.SOUTH);

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

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

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

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

            private void updateLineCount() {
                int lineCount = chatArea.getLineCount();
                if (lineCount <= CHAT_ROW_LIMIT) {
                    chatArea.setRows(lineCount);
                    mainPanel.revalidate();
                }
            }
        });

        JFrame f = new JFrame("ResizeTextArea");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(mainPanel);
        f.pack();
        f.setVisible(true);
    }
}

是 1 行、4 行和 8 行的情况:

1 行4 行8 行

I wrote a small program that resizes the JTextArea up to a maximum of 4 lines using only Swing controls

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class ResizeTextArea {

    public static final int CHAT_ROW_LIMIT = 4;

    public static void main(String[] args) {
        JPanel topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(200, 200));
        topPanel.setBackground(Color.WHITE);

        final JTextArea chatArea = new JTextArea();
        final JScrollPane scrollPane = new JScrollPane(chatArea);

        final JPanel mainPanel = new JPanel(new BorderLayout(5,5));
        mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        mainPanel.add(topPanel, BorderLayout.CENTER);
        mainPanel.add(scrollPane, BorderLayout.SOUTH);

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

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

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

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

            private void updateLineCount() {
                int lineCount = chatArea.getLineCount();
                if (lineCount <= CHAT_ROW_LIMIT) {
                    chatArea.setRows(lineCount);
                    mainPanel.revalidate();
                }
            }
        });

        JFrame f = new JFrame("ResizeTextArea");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(mainPanel);
        f.pack();
        f.setVisible(true);
    }
}

Here is how it looks for 1 line, 4 lines, and 8 lines:

1 line4 lines8 lines

酸甜透明夹心 2025-01-15 01:09:07

您可以将 DocumentFilter 添加到 jTextArea 的文档中。在过滤器中检查 jTextArea 的行计数,并根据行计数值允许/阻止添加文本。

You can add a DocumentFilter to the jTextArea's Document. IN the filter check row count of your jTextArea and allow/prevent text adding depending on the row count value.

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