简体   繁体   English

修复Java.Swing绝对布局错误

[英]Fixing Java.Swing absolute layout misposition

I'm currently using setLayout(null); 我目前正在使用setLayout(null); (my preference) but as usual, it's easy to run into positioning problems. (我的偏爱),但是像往常一样,很容易遇到定位问题。 In this case I've got a AWT.Chooser and 2 JButton 's. 在这种情况下,我有一个AWT.Chooser和2个JButton Like so, Good One Every so often upon launch the 2nd JButton "Cancel" will render the buttons Y position + it's height , Like so, Bad One From what I could find throughout Oracles tutorials and here on Stack, it's setLayout(null) that's causing this issue. 像这样, Good One经常在启动第二个JButton “ Cancel”时将按钮Y位置+它的height渲染出来,像这样, Bad One从我在整个Oracle教程中都能找到,而在Stack上,则是setLayout(null)导致此问题。 To fix it I've gone through various different layouts trying to find which one fits this particular design. 为了解决这个问题,我经历了各种不同的布局,试图找到哪种布局适合此特定设计。 One I found was CardLayout which seems to be the closest. 我发现的一个是CardLayout ,它似乎是最接近的。 Although getting the buttons to be in the right position doesn't fit my design. 尽管使按钮处于正确的位置不适合我的设计。 Is there a way to fix this without using a layout manager? 有没有不使用布局管理器即可解决此问题的方法? Am I taking the wrong approach to this? 我是否对此采取了错误的方法? I'm not picky, changing the window size if needed is doable and if a new design is needed, that's also not a problem. 我并不挑剔,可以根据需要更改窗口大小,并且如果需要新设计,那也不是问题。 Thanks in advance! 提前致谢!

One I found was CardLayout which seems to be the closest. 我发现的一个是CardLayout ,它似乎是最接近的。

CardLayout? CardLayout? That makes no sense as that is not a layout for component positioning but rather component swapping. 这没有意义,因为这不是组件定位的布局,而是组件交换。 Are you sure you don't mean another? 您确定您不是指另一个吗? Myself, I usually nest JPanels, each with its own layout manager, and this way easily create complex GUI's that are easy to maintain. 我本人通常会嵌套 JPanels,每个JPanels都有自己的布局管理器,并且这种方式可以轻松创建易于维护的复杂GUI。

在此处输入图片说明

For your GUI, I'd give the overall GUI a BorderLayout and add the central JPanel to the BorderLayout.CENTER position. 对于您的GUI,我将为整个GUI提供BorderLayout并将中央JPanel添加到BorderLayout.CENTER位置。 I'd create a JPanel that uses FlowLayout(FlowLayout.TRAILING) as its layout for the bottom, and add my JButtons to it, and that add that to the main JPanel in the BorderLayout.PAGE_END position. 我将创建一个使用FlowLayout(FlowLayout.TRAILING)作为其底部布局的JPanel,并向其中添加我的JButton,并将其添加到BorderLayout.PAGE_END位置的主JPanel中。 The possibilities of how to solve this are endless. 如何解决这个问题的可能性是无限的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM