Java JTextArea 中的滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1052473/
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-08-11 22:48:28  来源:igfitidea点击:

scrollbars in JTextArea

javaswing

提问by

How do I add scrollbars to a JTextArea?

如何将滚动条添加到 JTextArea?

回答by Fredrik

Put it in a JScrollPane

把它放在一个 JScrollPane 中

Edit: Here is a link for you: http://java.sun.com/docs/books/tutorial/uiswing/components/textarea.html

编辑:这是给你的链接:http: //java.sun.com/docs/books/tutorial/uiswing/components/textarea.html

回答by coobird

As Fredrik mentions in his answer, the simple way to achieve this is to place the JTextAreain a JScrollPane. This will allow scrolling of the view area of the JTextArea.

正如弗雷德里克在提到他的回答,简单的方法来实现,这是放置JTextAreaJScrollPane。这将允许滚动JTextArea.

Just for the sake of completeness, the following is how it could be achieved:

为了完整起见,以下是它的实现方式:

JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);   // JTextArea is placed in a JScrollPane.

Once the JTextAreais included in the JScrollPane, the JScrollPaneshould be added to where the text area should be. In the following example, the text area with the scroll bars is added to a JFrame:

JTextArea包含在 中后JScrollPaneJScrollPane应将其添加到文本区域应位于的位置。在以下示例中,带有滚动条的文本区域被添加到 a 中JFrame

JFrame f = new JFrame();
f.getContentPane().add(sp);

Thank you kd304 for mentioning in the comments that one should add the JScrollPaneto the container rather than the JTextArea-- I feel it's a common error to add the text area itself to the destination container rather than the scroll pane with text area.

感谢 kd304 在评论中提到应该将JScrollPane加到容器而不是JTextArea- 我觉得将文本区域本身添加到目标容器而不是带有文本区域的滚动窗格是一个常见的错误。

The following articles from The Java Tutorialshas more details:

The Java Tutorials 中的以下文章有更多详细信息:

回答by Norm MacLennan

You first have to define a JTextArea as per usual:

您首先必须像往常一样定义 JTextArea:

public final JTextArea mainConsole = new JTextArea("");

Then you put a JScrollPane over the TextArea

然后你在 TextArea 上放了一个 JScrollPane

JScrollPane scrollPane = new JScrollPane(mainConsole);
scrollPane.setBounds(10,60,780,500);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

The last line says that the vertical scrollbar will always be there. There is a similar command for horizontal. Otherwise, the scrollbar will only show up when it is needed (or never, if you use _SCROLLBAR_NEVER). I guess it's your call which way you want to use it.

最后一行表示垂直滚动条将始终存在。水平方向也有类似的命令。否则,滚动条只会在需要时显示(或者如果使用 _SCROLLBAR_​​NEVER,则永远不会显示)。我想这是你的电话,你想以哪种方式使用它。

You can also add wordwrap to the JTextArea if you want to:Guide Here

如果需要,您还可以将自动换行添加到 JTextArea:此处的指南

Good luck,
Norm M

祝你好运,
规范 M

P.S. Make sure you add the ScrollPane to the JPanel and not add the JTextArea.

PS 确保将 ScrollPane 添加到 JPanel 而不是添加 JTextArea。

回答by Bhushankumar Lilapara

            txtarea = new JTextArea(); 
    txtarea.setRows(25);
    txtarea.setColumns(25);
    txtarea.setWrapStyleWord(true);
    JScrollPane scroll = new JScrollPane (txtarea);
    panel2.add(scroll); //Object of Jpanel

Above given lines automatically shows you both horizontal & vertical Scrollbars..

上面给定的行会自动显示水平和垂直滚动条。

回答by user2538501

I just wanted to say thank you to the topmost first post by a user whom I think is named "coobird". I am new to this stackoverflow.com web site, but I cant believe how useful and helpful this community is...so thanks to all of you for posting some great tips and advise to others. Thats what a community is all about.

我只是想对我认为名为“coobird”的用户的最上面的第一篇文章表示感谢。我是这个 stackoverflow.com 网站的新手,但我无法相信这个社区是多么有用和有帮助……所以感谢你们所有人发布了一些很棒的技巧和建议给其他人。这就是社区的全部意义所在。

Now coobird correctly said:

现在coobird正确地说:

As Fredrik mentions in his answer, the simple way to achieve this is to place the JTextArea in a JScrollPane. This will allow scrolling of the view area of the JTextArea.

正如 Fredrik 在他的回答中提到的,实现这一点的简单方法是将 JTextArea 放在 JScrollPane 中。这将允许滚动 JTextArea 的视图区域。

I would like to say:

我想说:

The above statement is absolutely true. In fact, I had been struggling with this in Eclipse using the WindowBuilder Pro plugin because I could not figure out what combination of widgets would help me achieve that. However, thanks to the post by coobird, I was able to resolve this frustration which took me days.

以上说法完全正确。事实上,我一直在使用 WindowBuilder Pro 插件在 Eclipse 中为此苦苦挣扎,因为我无法弄清楚哪种小部件组合可以帮助我实现这一目标。然而,多亏了 coobird 的帖子,我才能够解决这个花了我好几天的挫败感。

I would also like to add that I am relatively new to Java even though I have a solid foundation in the principles. The code snippets and advise you guys give here are tremendously useful.

我还想补充一点,尽管我对 Java 的原则有扎实的基础,但我对 Java 还是比较陌生。你们在这里给出的代码片段和建议非常有用。

I just want to add one other tid-bit that may help others. I noticed that Coobird put some code as follows (in order to show how to create a Scrollable text area). He wrote:

我只想添加另一个可能对其他人有帮助的花絮。我注意到 Coobird 把一些代码如下(为了展示如何创建一个可滚动的文本区域)。他写了:

JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);   

I would like to say thanks to the above code snippet from coobird. I have not tried it directly like that but I am sure it would work just fine. However, it may be useful to some to let you know that when I did this using the WindowBuilder Pro tool, I got something more like the following (which I think is just a slightly longer more "indirect" way for WindowBuilder to achieve what you see in the two lines above. My code kinda reads like this:

我想感谢上面来自 coobird 的代码片段。我没有像那样直接尝试过,但我相信它会工作得很好。但是,让某些人知道当我使用 WindowBuilder Pro 工具执行此操作时,我得到的内容更类似于以下内容(我认为这只是 WindowBuilder 实现您的目标的稍微更长的“间接”方式)可能对某些人有用请看上面的两行。我的代码有点像这样:

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(23, 40, 394, 191);
frame.getContentPane().add(scrollPane);

JTextArea textArea_1 = new JTextArea();
scrollPane.setViewportView(textArea_1);`

Notice that WindowBuilder basically creates a JScrollPane called scrollpane (in the first three lines of code)...then it sets the viewportview by the following line: scrollPane.setViewportView(textArea_1). So in essence, this line is adding the textArea_1 in my code (which is obviously a JTextArea) to be added to my JScrollPane **which is precisely what coobird was talking about).

请注意,WindowBuilder 基本上创建了一个名为 scrollpane 的 JScrollPane(在前三行代码中)...然后它通过以下行设置视口视图:scrollPane.setViewportView(textArea_1)。所以本质上,这一行是在我的代码中添加 textArea_1(这显然是一个 JTextArea)以添加到我的 JScrollPane **这正是 coobird 所说的)。

Hope this is helpful because I did not want the WindowBuilder Pro developers to get confused thinking that Coobird's advise was not correct or something.

希望这会有所帮助,因为我不希望 WindowBuilder Pro 开发人员感到困惑,认为 Coobird 的建议不正确或其他什么。

Best Wishes to all and happy coding :)

祝大家编码愉快:)

回答by Sachindra N. Pandey

Simple Way to add JTextArea in JScrollBar with JScrollPan

使用 JScrollPan 在 JScrollBar 中添加 JTextArea 的简单方法

import javax.swing.*;
public class ScrollingTextArea 
{
     JFrame f;
     JTextArea ta;
     JScrollPane scrolltxt;

     public ScrollingTextArea() 
     {
        // TODO Auto-generated constructor stub

        f=new JFrame();
        f.setLayout(null);
        f.setVisible(true);
        f.setSize(500,500);
        ta=new JTextArea();
        ta.setBounds(5,5,100,200);

        scrolltxt=new JScrollPane(ta);
        scrolltxt.setBounds(3,3,400,400);

         f.add(scrolltxt);

     }

     public static void main(String[] args) 
     {
        new ScrollingTextArea();
     }
}