如何强制 JScrollPane 仅垂直滚动?
伙计们,我需要在 jscrollpanel 中放置一些按钮,但 JScrollPane 不会创建垂直滚动。我在 JScrollPane 中使用 JPanel,它使用简单的 FlowLayout 布局。如何使 JScrollPanel 仅在垂直方向滚动?
问题:
所需的解决方案:
Guys, I need to put some buttons in a jscrollpanel, but the JScrollPane won't create a scroll vertically. I'm using a JPanel inside the JScrollPane which is using the simple FlowLayout layout. How can I make the JScrollPanel to scroll only in the vertical??
Problem:
Desired Solution:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
查看换行布局
Check out the Wrap Layout
事实上您使用
JScrollPane
改变了很多有关内部 <代码>FlowLayout。事实上,当 FlowLayout 尝试布局包含的 JButton 时,它会使用可用空间。就您而言,您的 JScrollPane 的“可滚动客户端”中的空间没有限制。因此,考虑到您的 FlowLayout 有无限的空间,它使用该空间来根据它显示项目。因此,解决方案是更改可滚动客户端,以将其可视区域限制为与 JScrollPane 的
JViewport
。但是,在这种情况下,您甚至不会有行返回,因为 FlowLayout 不能很好地处理这种情况。
如果我是你,我当然会选择其他布局。由于 GridLayout 不能很好地处理边框,我认为您可以使用的唯一合理的标准布局是 GridBagLayout ,尽管我担心您的动态内容限制可能需要您做更多的事情可定制。
The fact you use a
JScrollPane
changes quite a few things concerning the internalFlowLayout
. indeed, when the FlowLayout tries to layout contained JButtons, it use for that the available space. In your case, you don't have limits to the space in the "scrollable client" of your JScrollPane. As a consequence, considering your FlowLayout has infinite space, it uses this space to display items according to it.So the solution would be to change your scrollable client in order to limit its viewable area to the same than your JScrollPane's
JViewport
.However, you would not even in this case have your line returns, as FlowLayout don't really well handle this case.
Were I to be you, I would of course choose an other layout. As
GridLayout
don't really well handles borders, i think the only reasonible standard layout you can use isGridBagLayout
, althgough I fear your dynamic content constraints may require you something even more customizable.这会将文本区域中的所有内容换行到下一行,而不创建水平滚动。
This will wrap anything in a text area to the next line without creating a Horizontal Scroll.
使用我在此答案中发布的修改后的流布局: 如何让 JToolBars 换行到下一行(FlowLayout)而不隐藏它们下面的 JPanel?
它将换行到下一行,并且滚动条应该垂直滚动。
Use the modified Flow Layout that I posted in this answer: How can I let JToolBars wrap to the next line (FlowLayout) without them being hidden ty the JPanel below them?
It will wrap to the next line and your scrollbar should scroll vertically.
或者您可以使用 JList。
有关详细信息,请参阅此站点:http://docs.oracle.com/ javase/tutorial/uiswing/components/list.html
示例类:ListDialog 当窗口大小调整或元素不移动时仅使用垂直滚动条不适合视图。
Or you could use a JList.
See this site for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html
the example class: ListDialog uses only a vertical scrollbar, when the window is resized or the elements don't fit the view.