java 如何强制 JScrollPane 仅垂直滚动?

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

How do I force JScrollPane to only scroll vertical?

javaswingjscrollpane

提问by Marcos Roriz Junior

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??

伙计们,我需要在 jscrollpanel 中放置一些按钮,但 JScrollPane 不会垂直创建滚动。我在 JScrollPane 中使用了一个 JPanel,它使用了简单的 FlowLayout 布局。我怎样才能让 JScrollPanel 只在垂直方向滚动??

Problem:

问题:

enter image description here

在此处输入图片说明

Desired Solution: enter image description here

所需的解决方案: 在此处输入图片说明

采纳答案by camickr

Check out the Wrap Layout

查看环绕布局

回答by RainMain

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.

这会将文本区域中的任何内容包装到下一行,而不会创建水平滚动。

回答by Riduidel

The fact you use a JScrollPanechanges quite a few things concerning the internal FlowLayout. 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.

你使用 a 的事实JScrollPane改变了很多关于内部的事情FlowLayout。实际上,当 FlowLayout 尝试布局包含 JButton 时,它会使用可用空间。在您的情况下,您对 JScrollPane 的“可滚动客户端”中的空间没有限制。因此,考虑到您的 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 GridLayoutdon'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,尽管我担心您的动态内容约束可能需要您更可定制的东西。

回答by jzd

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.

它将换行到下一行,您的滚动条应垂直滚动。

回答by emeraldjava

scrollbar = new Scrollbar(Scrollbar.VERTICAL);

回答by SuperRetro

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: ListDialoguses only a vertical scrollbar, when the window is resized or the elements don't fit the view.

示例类:ListDialog在调整窗口大小或元素不适合视图时仅使用垂直滚动条。