当文本填充了组件而不是具有滚动条时,如何在Java Netbeans中动态调整文本组件的大小? [英] How do I dynamically resize a text component in Java Netbeans, when the text has filled the component, instead of it having scroll bars?

查看:124
本文介绍了当文本填充了组件而不是具有滚动条时,如何在Java Netbeans中动态调整文本组件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个问题,我已经做了大量研究,但是对于发现的任何东西我都没有任何运气.这是我正在开发的应用程序中的主要障碍.

I've done a ton of research regarding this issue, but I haven't had any luck with anything I've found. It's a major roadblock in the application I'm developing.

我的应用程序有一个Form,其中包含JScrollPane.在JScrollPane中添加JTextArea之后,我看到当我运行该程序并键入JTextArea时,当JTextArea溢出文本时,它会伴随着滚动条.即使我将滚动条策略设置为从不",它也只会溢出并向上移动当前文本.

My application has a Form, which contains a JScrollPane. After adding a JTextArea inside the JScrollPane, I see that when I run the program and type in the JTextArea, it is accompanied by scrollbars when the JTextArea overflows with text. Even if I set the scrollbar policy to 'NEVER', it simply overflows and moves the current text up.

我希望我的JTextArea所做的只是在文本内容变得太大而无法容纳在框中时,大小(垂直/高度)增加.这似乎是一个非常简单的概念-即使在MS Paint中,我们也能看到一些东西.添加文本对象时,它是特定大小.然后,作为输入的文本,每一行都使垂直方向上的文本字段变大.文本字段也可以由用户手动调整大小.我现在不需要做此功能.

What I would like my JTextArea to do is simply grow in size (vertically/height wise) when the textual content becomes too much to fit in the box. This seems to be a fairly simple concept - something we see, even in MS Paint. When a text object is added, it is a specific size. Then, as text as entered, each new line makes the text field larger, vertically. The text field can also be resized manually by the user. This feature isn't something I will need to do right now.

我尝试使用jTextArea1.getSize()获取和设置大小,以及使用jTextArea1.getRows()获取行数,使用jTextArea1.getLineCount()获取行数,并使用jTextArea1.getPreferredSize()设置首选大小.

I have tried getting and setting the size using jTextArea1.getSize(), as well as the row count using jTextArea1.getRows(), line count using jTextArea1.getLineCount(), and the preferred size using jTextArea1.getPreferredSize().

我尝试使用这些返回的变量通过jTextArea1.setSize()jTextArea1.setRows()jTextArea1.setLineCount()jTextArea1.setPreferredSize()来设置大小,但没有一个起作用.关于我在做什么错的任何建议吗?

I have tried to use these returned variables to set the size using jTextArea1.setSize(), jTextArea1.setRows(), jTextArea1.setLineCount(), and jTextArea1.setPreferredSize(), none of which have worked. Any advice on what I'm doing wrong?

推荐答案

添加新文本后,只需revalidate()文本组件的父级即可.

Just revalidate() the parent of the text component after adding the new text.

这篇关于当文本填充了组件而不是具有滚动条时,如何在Java Netbeans中动态调整文本组件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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