[英]How to align a gridbaglayout with components to the top left of the JPanel they are in
[英]How to make Components align to the TOP in JPanel with BoxLayout?
我正在开发一款名为GalaxyWar的游戏,我正在尝试制作地图选择菜单。 我发现一个问题,当我在带有setAlignmentX(CENTER_ALIGNMENT)
的JPanel上使用BoxLayout和BoxLayout.Y_AXIS
时,具有指定大小的子组件(JPanel)占据了面板的整个高度(全部在一起),而不是分配的高度!
这是我的代码:
scrollPane = new JScrollPane();
scrollPane.setBounds(160, 11, 452, 307);
add(scrollPane);
mapContainer = new JPanel();
mapContainer.setAlignmentX(CENTER_ALIGNMENT);
mapContainer.setAlignmentY(JPanel.TOP_ALIGNMENT);
mapContainer.setLayout(new BoxLayout(mapContainer, BoxLayout.Y_AXIS));
scrollPane.setViewportView(mapContainer);
JPanel demoPanel = new JPanel();
demoPanel.setLayout(null);
demoPanel.setBackground(Color.YELLOW);
demoPanel.setSize(50, 100);
mapContainer.add(demoPanel);
我已经对此进行了长时间的研究,但到目前为止找不到任何解决方案。
试着看看
setPreferredSize()
setMaximumSize()
setMinimumSize()
将所有3设置为相同的值。
如果它仍然不起作用,您可以尝试将您尝试将其大小设置为固定的面板放在另一个面板中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.