JSeparator 不在我的 JPanel 中的正确位置
所以我的代码如下:
JPanel mainPanel = new JPanel();
mainPanel.setBorder(new EmptyBorder(50,50,0,10));
BoxLayout layout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
mainPanel.setLayout(layout);
JSeparator separate = new JSeparator(SwingConstants.HORIZONTAL);
mainPanel.add(separate);
mainPanel.add(new JButton());
mainPanel.add(new JButton());
我一直遇到的问题是,我的面板看起来不像:
______________
| |
| ------ |
| Button |
| Button |
| |
| |
| |
______________
它出于某种原因在按钮和分隔符之间放置了大量空间,所以它看起来像:
______________
| |
| ------ |
| |
| |
| |
| Button |
| Button |
______________
对于我的生活,我可以'不让按钮位于 JSeparator 旁边,有什么想法吗?
So my code is as follows:
JPanel mainPanel = new JPanel();
mainPanel.setBorder(new EmptyBorder(50,50,0,10));
BoxLayout layout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
mainPanel.setLayout(layout);
JSeparator separate = new JSeparator(SwingConstants.HORIZONTAL);
mainPanel.add(separate);
mainPanel.add(new JButton());
mainPanel.add(new JButton());
the problem that I keep having is that instead of my panel looking like:
______________
| |
| ------ |
| Button |
| Button |
| |
| |
| |
______________
it for some reason puts a ton of space in between the buttons and separator so it looks like:
______________
| |
| ------ |
| |
| |
| |
| Button |
| Button |
______________
For the life of me I can't get the buttons to be next to the JSeparator, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BoxLayout 尊重组件的最大尺寸。当有更多可用空间时,组件将增长以占用额外的空间。您需要防止分隔符增长:
BoxLayout respects the maximum size of the component. When there is more space available the component will grow to take up the extra space. You need to prevent the separator from growing:
如果 Y 对齐(来自 .getAlignmentY())不相同,BoxLayout 往往会做一些奇怪的事情。尝试手动将对齐方式设置为顶部。 (BoxLayout 中的 X 对齐也会发生同样的情况。)
If the the Y alignments (from .getAlignmentY()) aren't the same, BoxLayout tends to do funky things. Try manually setting the alignments to the top. (Same thing happens with the X alignment in BoxLayout.)