I'm using GridBagLayout and JPanel 2 seems to always be in the center of the JPanel 1. JPanel 2 gets dinamically adjusted in height throughout the program, so I can't stick it to the top using gbc.insets.top = -15;
(gbc = GridBagConstraints) for example. I've tried gbc.anchor = GridBagConstraints.PAGE_START;
or gbc.gridheight = 3;
but they don't seem to do what I'm looking for.
假设JPanel 1和JFrame保持相同的大小(JFrame不可调整大小),我如何才能使JPanel 2始终位于顶部,而又没有这个间隙呢?
然后,您似乎正在向面板2添加面板,因为GridBagLayout的默认行为是使组件相对于其父组件居中。
Based on the picture shown just use the
BorderLayout
on the frame (which is the default). Then you would:BorderLayout.LINE_START
BorderLayout.CENTER
Read the section from the Swing tutorial on How to Use BorderLayout for more information.