JTextArea中的滚动条 [英] scrollbars in JTextArea

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

问题描述

如何将滚动条添加到JTextArea?

How do I add scrollbars to a JTextArea?

推荐答案

正如Fredrik在他的答案,实现这一目标的简单方法是放置 JTextArea //java.sun.com/javase/6/docs/api/javax/swing/JScrollPane.html\"rel =noreferrer> JScrollPane 。这将允许滚动 JTextArea的视图区域

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.

为了完整起见,以下是如何实现:

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.

一旦 JTextArea 包含在 JScrollPane ,应将 JScrollPane 添加到文本区域的位置。在以下示例中,带有滚动条的文本区域将添加到 JFrame

Once the JTextArea is included in the JScrollPane, the JScrollPane should 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:

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

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

Thank you kd304 for mentioning in the comments that one should add the JScrollPane to 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.

以下文章来自 Java教程有更多细节:

The following articles from The Java Tutorials has more details:

  • How to Use Scroll Panes
  • How to Use Text Areas

这篇关于JTextArea中的滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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