简体   繁体   English

Java:jscrollpane禁用水平滚动

[英]Java : jscrollpane disable horizontal scrolling

I want to add a Jpanel on a jscrollpane; 我想在jscrollpane上添加一个Jpanel; also I want to have only vertical scrolling. 我也只想垂直滚动。 I want to set layout of my jPanel "flowLaout" and add several components to my jPanel in my code by jpanel.add(component) method. 我想设置jPanel“ flowLaout”的布局,并通过jpanel.add(component)方法在我的代码中向jPanel添加几个组件。 The result is that all components are placed in just one row that excide width of jpanel and are not shown. 结果是所有组件仅放置在除jpanel宽度之外的一行中,并且未显示。 I have used this tricks and both failed: 我已经使用了这些技巧,但都失败了:

jScrollPane1.setHorizontalScrollBar(null);
jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

Wrap Layout应该适合您。

I am unsure of the particulars for your current project, but i would recommend MigLayout . 我不确定您当前项目的细节,但我建议使用MigLayout It has never served me wrong. 它从来没有为我服务。

I am currently writing a touchscreen interface with nested MigLayout panels up to 4 or five layers deep, and have not had a single problem. 我目前正在编写一个触摸屏界面,该界面具有嵌套的MigLayout面板,其深度最多为4或5层,并且没有任何问题。

Please use the below policy to turn on vertical scrolling and turn off horizontal scrolling(Works with Java SE 7): 请使用以下策略打开垂直滚动并关闭水平滚动(适用于Java SE 7):

Panel graphicPanel = new Panel();
JScrollPane scrollbar = new JScrollPane(graphicPanel);
scrollbar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollbar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollbar.setPreferredSize(new Dimension(1300, 600));
scrollbar.setVisible(true);
add(scrollbar, BorderLayout.CENTER);

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

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