java 如何将 jTextArea 设置为具有与其包含的文本大小相匹配的高度(以避免滚动条)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3081210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:16:54  来源:igfitidea点击:

How to set jTextArea to have height that matches the size of a text it contains (to avoid scrollbars)

javaswingjscrollpanejtextareajscrollbar

提问by celicni

This problem looks trivial, but I can't find the solution.

这个问题看起来微不足道,但我找不到解决方案。

When I create a form it contains a JTextArea. I want to put large constant text in it. If the text is say 1000 lines long, I want my JTextAreato be 1000 lines high (to be large enough to display the entire text without scrollbars). JTextAreais inside a panel which can have scrollbar so it is not a problem when it gets too large (there are two JTextAreain that panel.. something like in a diff tool). Does anybody knows how can I achieve this? Thanks.

当我创建一个表单时,它包含一个JTextArea. 我想在其中放入大的常量文本。如果文本长 1000 行,我希望我的文本JTextArea高 1000 行(足够大以显示没有滚动条的整个文本)。JTextArea位于一个可以有滚动条的面板内,因此当它变得太大时它不是问题(JTextArea该面板中有两个......类似于差异工具中的东西)。有谁知道我怎样才能做到这一点?谢谢。

采纳答案by akf

The BorderLayoutwill handle the scrollbar out of the box if you simply put your JTextAreain a JScrollPanebefore you add it to your JPanel. FlowLayout, on the other hand, does not. It will not display the scroll bar unless, as @Xorty intimates, you call setPreferedSize()on your JScrollPaneand give it the dimension that you would like.

BorderLayout将处理滚动条开箱,如果你只是把你JTextAreaJScrollPane,你把它添加到你的面前JPanelFlowLayout,另一方面,没有。它不会显示滚动条,除非,正如@Xorty 暗示的那样,你调用setPreferedSize()你的JScrollPane并给它你想要的尺寸。

回答by Taisin

You can also use something like this (limited width, height depending on text, useful when showing info messages):

你也可以使用这样的东西(有限的宽度,高度取决于文本,在显示信息消息时很有用):

  public JTextArea createTextAreaFitToText(String message, Dimension minimalSize){

        JTextArea aMessagePanel = new JTextArea();
        aMessagePanel.setText(message);

        /*for modelToView to work, the text area has to be sized. It doesn't matter if it's visible or not.*/
        aMessagePanel.setPreferredSize(minimalSize);
        aMessagePanel.setSize(minimalSize);            

        Rectangle r = aMessagePanel.modelToView(aMessagePanel.getDocument().getLength()); 

        Dimension d = new Dimension(minimalSize.width, r.y + r.height);
        aMessagePanel.setPreferredSize(d);
        return aMessagePanel;

}

回答by sushant goel

To increase or decrease the height of JTextArea. When a text is entered, call for getPreferredSize() of JTextArea- it'll give you the size needed to display the whole text. After that use setPrefferedSize() of JScrollPane to set the size of JTextArea

增加或减少 JTextArea 的高度。当输入文本时,调用 JTextArea 的 getPreferredSize() - 它会给你显示整个文本所需的大小。之后使用 JScrollPane 的 setPrefferedSize() 来设置 JTextArea 的大小

回答by Xorty

Well, first of all : it's JTextArea not jTextArea.

好吧,首先:它是 JTextArea 而不是 jTextArea。

Now - put JTextArea into JScrollPane. When a text is entered, call for getPreferedSize()of JScrollPane - it'll give you precise size needed to display whole text. Also, I never use other LayoutManager than 'Free Design' from NetBeans Swing builder - so I am not sure how other LayoutManagers will behave there.

现在 - 将 JTextArea 放入 JScrollPane。当输入文本时,调用getPreferedSize()JScrollPane - 它会给你显示整个文本所需的精确大小。此外,除了来自 NetBeans Swing 构建器的“自由设计”之外,我从不使用其他 LayoutManager - 所以我不确定其他 LayoutManager 在那里的表现。