使 JtextPane 看起来像电子书
我有一个带有 Netbeans 的文本编辑器,我可以将文本加载到 JtextPane 中。如果文本太大,您可以借助水平滚动来阅读它。有没有办法将文本分成 24 行的页面,例如,以便每个页面都可见而无需滚动,并使用下一页按钮来更改页面(就像电子书一样)?
I have a text editor with Netbeans where i load a text to a JtextPane. If text is too big u can read it with the help of an horizontal scroll.Is there any way to split the text into pages of 24 lines for example so that every page is visible without scrolling and use a next page button for changing page (like eBooks do)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

使用 JTextArea 来执行此操作更容易,因为您可以轻松指定每次滚动到新页面时要显示的行数。
基本解决方案是将文本区域添加到滚动窗格,然后隐藏滚动条。然后,您可以使用垂直滚动条的默认操作来为您进行滚动。下面的代码使用操作地图操作博客条目中的代码轻松创建可添加到 JButton 的 Action:
It is easier to use a JTextArea to do this because you can easily specify the number of lines to display each time you scroll to a new page.
The basic solution is to add a text area to a scroll pane than then hide the scrollbars. You can then use the defaults Actions of the vertical scrollbar to do the scrolling for you. The code below uses code from the Action Map Action blog entry to easily create an Action that you can add to a JButton: