简体   繁体   English

如何强制JScrollPane只滚动垂直?

[英]How do I force JScrollPane to only scroll vertical?

Guys, I need to put some buttons in a jscrollpanel, but the JScrollPane won't create a scroll vertically. 伙计们,我需要在jscrollpanel中放置一些按钮,但JScrollPane不会垂直创建滚动。 I'm using a JPanel inside the JScrollPane which is using the simple FlowLayout layout. 我在JScrollPane中使用JPanel,它使用简单的FlowLayout布局。 How can I make the JScrollPanel to scroll only in the vertical?? 如何让JScrollPanel只在垂直方向滚动?

Problem: 问题:

在此输入图像描述

Desired Solution: 期望的解决方案: 在此输入图像描述

查看自动换行布局

The fact you use a JScrollPane changes quite a few things concerning the internal FlowLayout . 您使用JScrollPane的事实改变了有关内部FlowLayout的一些事情。 indeed, when the FlowLayout tries to layout contained JButtons, it use for that the available space. 实际上,当FlowLayout尝试布局包含的JButton时,它会使用可用空间。 In your case, you don't have limits to the space in the "scrollable client" of your JScrollPane. 在您的情况下,您对JScrollPane的“可滚动客户端”中的空间没有限制。 As a consequence, considering your FlowLayout has infinite space, it uses this space to display items according to it. 因此,考虑到FlowLayout具有无限空间,它会使用此空间根据它显示项目。

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 . 所以解决方案是更改可滚动客户端,以便将其可视区域限制为与JScrollPane的JViewport相同。

However, you would not even in this case have your line returns, as FlowLayout don't really well handle this case. 但是,在这种情况下你甚至不会有你的行返回,因为FlowLayout不能很好地处理这种情况。

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 is GridBagLayout , althgough I fear your dynamic content constraints may require you something even more customizable. 由于GridLayout不能很好地处理边框,我认为你可以使用的唯一合理的标准布局是GridBagLayout ,尽管我担心你的动态内容限制可能需要你更可定制的东西。

JTextArea c = new JTextArea();
c.setLineWrap(true);
c.setWrapStyleWord(false);

This will wrap anything in a text area to the next line without creating a Horizontal Scroll. 这将在文本区域中将任何内容包装到下一行,而不创建水平滚动。

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? 使用我在这个答案中发布的修改后的Flow Layout: 如何让JToolBars包装到下一行(FlowLayout)而不将它们隐藏在它们下面的JPanel中?

It will wrap to the next line and your scrollbar should scroll vertically. 它将换行到下一行,您的滚动条应垂直滚动。

scrollbar = new Scrollbar(Scrollbar.VERTICAL);

Or you could use a JList . 或者你可以使用JList

See this site for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/list.html 有关详细信息,请访问此站点: 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. 示例类:当调整窗口大小或元素不适合视图时, ListDialog仅使用垂直滚动条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM