我有一个jscrollpane,当我加载我的应用时,栏坐在我的一个按钮的顶部。我想做的是在我的按钮的一侧添加一些空间,以便滚动条在空间上绘制,而不是我的按钮。

我尝试的示例代码:

JPanel eButton = new JPanel(new BorderLayout());
JPanel spaceFiller = new JPanel();
spaceFiller.setSize(30, 10);
eButton.add(editButton, BorderLayout.EAST);
eButton.add(spaceFiller, BorderLayout.WEST);
.

此代码的问题是它仍然覆盖了我的按钮,没有添加空格。确保JScrollPane在JFrame中没有重叠组件的最佳方法是什么?

感谢

有帮助吗?

解决方案

To ensure that the size of the JPanel is respected you should use setPreferredSize() instead of setSize().

其他提示

In your sample code, didn't you reverse EAST and WEST? Shouldn't it be like:

eButton.add(editButton, BorderLayout.WEST); 
eButton.add(spaceFiller, BorderLayout.EAST); 

That would make more sense, sicne the scrollbar will appear on the right side (EAST).

Please note that the solution you suggest, even though it may work (after exchanging EAST and WEST) looks more like a hack than a real solution.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top