Java 在 jscrollpane 中禁用水平滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1727840/
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
disable horizontal scroll in jscrollpane
提问by phunehehe
I have a JScrollPane
with FlowLayout
that I want to have a fixed width. It should be scrolling vertically only, and the contents should be rearranged automatically when I resize the window. I think there should be a method like setEnableHorizontalScroll(false)
but can't find it.
我有一个JScrollPane
与FlowLayout
我希望有一个固定的宽度。它应该只垂直滚动,并且当我调整窗口大小时应该自动重新排列内容。我认为应该有一种方法,setEnableHorizontalScroll(false)
但找不到。
Is there any easy way to do this?
有什么简单的方法可以做到这一点吗?
采纳答案by phunehehe
Finally I found out how, please see this very short example, no advanced tricks needed:
最后我找到了方法,请看这个非常简短的例子,不需要高级技巧:
public class MyTest extends JFrame {
public static void main(String[] args) {
MyTest test = new MyTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setSize(300, 200);
test.setVisible(true);
}
public MyTest() {
String[] data = {
"Arlo", "Cosmo", "Elmo", "Hugo",
"Jethro", "Laszlo", "Milo", "Nemo",
"Otto", "Ringo", "Rocco", "Rollo"
};
JList list = new JList(data);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(0);
JScrollPane scroll = new JScrollPane(list);
this.add(scroll);
}
}
Adapted from Sun's tutorial
改编自Sun 的教程
回答by Matthew Murdoch
You can use:
您可以使用:
import static javax.swing.ScrollPaneConstants.*;
// ...
JScrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
...but this just prevents the horizontal scrollbar from ever being shown.
...但这只会阻止水平滚动条被显示。
To get the contents to rearrange automatically will depend on what the content is. A JTextPane
or JEditorPane
will do this for you automatically (without the need for the above code).
让内容自动重新排列将取决于内容是什么。A JTextPane
orJEditorPane
会自动为你做这件事(不需要上面的代码)。
回答by camickr
This is no custom solution out of the box in the JDK.
这不是 JDK 中开箱即用的自定义解决方案。
You can use the WrapLayout.
您可以使用WrapLayout。
Or you can create a custom panel and implement the Scrollable interface. The key in this case is overriding getScrollableTracksViewportWidth() to return true, so the viewports width and not the width of the panel is used for layout purposes. An example of this approach can be found in the ScrollableFlowPanel
或者您可以创建自定义面板并实现 Scrollable 接口。这种情况下的关键是覆盖 getScrollableTracksViewportWidth() 以返回 true,因此视口宽度而不是面板的宽度用于布局目的。可以在ScrollableFlowPanel 中找到此方法的示例
回答by Kevin Parkerson
I came across this issue as well... my solution was to modify the JScrollPane source codeand pass an additional parameter to disable horizontal scrolling. The edits are really simple:
我也遇到了这个问题……我的解决方案是修改JScrollPane 源代码并传递一个附加参数来禁用水平滚动。编辑非常简单:
On line 53:
在第 53 行:
$.fn.jScrollPane = function (settings, horizontalScrollingEnabled) {
Then, on line 177:
然后,在第 177 行:
if (horizontalScrollingEnabled) {
isScrollableH = percentInViewH > 1;
} else {
isScrollableH = false;
}
That's all you need to change. Minify the modified source code and include it on your page. When initializing the jScrollPane, you can now set whether you want it to scroll horizontally like so:
这就是你需要改变的全部。缩小修改后的源代码并将其包含在您的页面中。初始化 jScrollPane 时,您现在可以设置是否希望它像这样水平滚动:
$('.scroll-pane').jScrollPane("", false);
Hope that helps.
希望有帮助。
回答by aBenChase
Just set the display to "none" using the CSS class for the horizontal bar. At the bottom of your jScrollPlane (after you've initialized it), add in:
只需使用水平条的 CSS 类将显示设置为“无”。在 jScrollPlane 的底部(初始化之后),添加:
$('.jspHorizontalBar').css({ 'display': 'none' });
If using multiples on a page, and some that NEED the horizontal bar, you can always edit your targeting to include the parent element.
如果在页面上使用多个,并且一些需要水平栏,您可以随时编辑定位以包含父元素。
回答by whitesiroi
Just delete isScrollableH = percentInViewH > 1;
in jquery.jscrollpane.js
只需isScrollableH = percentInViewH > 1;
在 jquery.jscrollpane.js 中 删除
回答by cmicat
You can use org.jdesktop.swingx.JXPanel instead of javax.swing.JPanel as the ViewportView of your JScrollPane:
您可以使用 org.jdesktop.swingx.JXPanel 而不是 javax.swing.JPanel 作为 JScrollPane 的 ViewportView:
1.download "SwingX 1.6.4 All - Binary" , and add the jar to your project's Libraries here.
1.下载“SwingX 1.6.4 All - Binary”,然后将 jar 添加到您项目的库中。
2.set JXPanel as the ViewportView of your JScrollPane instead of a JPanel:
2.将 JXPanel 设置为 JScrollPane 的 ViewportView 而不是 JPanel:
JScrollPane scrollPane = new JScrollPane();
JXPanel panel = new JXPanel();
scrollPane.setViewportView(panel);
3.add following two statements:
3.添加以下两条语句:
panel.setScrollableTracksViewportHeight(false);
panel.setScrollableTracksViewportWidth(true);
4.then you can add elements to JXPanel as you did to a JPanel
4.然后你可以像添加 JPanel 一样向 JXPanel 添加元素
回答by erica may
try this one, this works fine on me jScrollPane1.setHorizontalScrollBar(null);
试试这个,这对我来说很好用 jScrollPane1.setHorizontalScrollBar(null);