Java ScrollPane 重叠内容
我确信我的 JScrollPanes 遇到了一个初学者问题。问题在于垂直滚动条与封闭面板(右侧)内的组件重叠。当滚动条与 JComboBox 的下拉位重叠时,这会变得有点痛苦。
我已将问题归结为这个小片段 - 我希望它能说明问题。
public class ScrollTest extends JFrame
{
public ScrollTest()
{
super("Overlap issues!");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(100,0));
for(int b=0;b<100;++b)
{
panel.add(new JButton("Small overlap here ->"));
}
JScrollPane scrollpane = new JScrollPane(panel);
add(scrollpane);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new ScrollTest();
}
}
我先看了一下,但不知道是否有其他人已经解决了这个问题。抱歉,如果它是重复的,非常感谢任何人都可以提供像我这样的 java-newb 的帮助!
I'm having what I am sure is very much a beginners problem with my JScrollPanes. The problem is that the vertical scrollbar overlaps the components within the enclosed panel (on the right hand side). It becomes a bit of a pain when the scrollbar overlaps the drop-down bit of JComboBoxes.
I have boiled the problem down to this little snippet - I hope it illustrates the issue.
public class ScrollTest extends JFrame
{
public ScrollTest()
{
super("Overlap issues!");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(100,0));
for(int b=0;b<100;++b)
{
panel.add(new JButton("Small overlap here ->"));
}
JScrollPane scrollpane = new JScrollPane(panel);
add(scrollpane);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new ScrollTest();
}
}
I had a look first but couldn't see if anyone else had already addressed this problem. Sorry if it's a duplicate and many thanks for any help anyone can offer a java-newb like me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,JScrollPane 的默认设置是使用默认值 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED 来布局组件,这反过来会添加滚动条,而无需再次布局组件。
在您的示例中,您知道您将需要一个滚动条,因此将其更改为始终显示滚动条
The problem is that the default for a JScrollPane is to layout the components with a default of JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED which in turn adds the scrollbar without laying out the components again.
In your example you know you will need a scrollbar so change it to always display the scrollbar