如何向 JPanel 添加空间,以便 JScrollPane 不会位于我的组件之上?
我有一个 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 中的组件重叠的最佳方法是什么?
谢谢
I have a JScrollPane and when I load my application the bar is sitting on top of one of my buttons. What I would like to do is add some space to the side of my button so that the scroll bar draws over the space and not my button.
Example code that I tried:
JPanel eButton = new JPanel(new BorderLayout());
JPanel spaceFiller = new JPanel();
spaceFiller.setSize(30, 10);
eButton.add(editButton, BorderLayout.EAST);
eButton.add(spaceFiller, BorderLayout.WEST);
The problem with this code is that it still overwrites my button and no space is added. What is the best way to make sure that JScrollPane doesn't overlap the components in my JFrame?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了确保 JPanel 的大小得到尊重,您应该使用 setPreferredSize() 而不是 setSize()。
To ensure that the size of the JPanel is respected you should use setPreferredSize() instead of setSize().
在你的示例代码中,你没有颠倒EAST和WEST吗?难道不应该是这样的:
这会更有意义,因为滚动条将出现在右侧(东)。
请注意,您建议的解决方案,即使它可能有效(在交换 EAST 和 WEST 之后),看起来更像是一个黑客而不是真正的解决方案。
In your sample code, didn't you reverse EAST and WEST? Shouldn't it be like:
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.