当JPanel消失时,可resize的布局摆动

我有很多jPanel(panelF)添加到其他jPanel(panelB)。 jPanelF包含jPanelC。 在此处输入图像描述

我已设置布局以添加它们如下:

PanelB.setLayout(new BoxLayout(this.PanelB, BoxLayout.PAGE_AXIS)); PanelF panelF1 = new PanelF(); PanelB.add(panelF1); PanelF panelF2 = new PanelF(); PanelB.add(panelF2); 

我在jPanelC1中设置了可见的false。 但是JPanelF2保留了距离而不想制作那个空白区域

在此处输入图像描述 我想要的是在消失JPanelC1时。 jPanelF之间保持距离而不是空白区域。 我尝试了validate()并更改了布局但是我失败了。 会是什么样的? 非常感谢你。 对不起我的英语不好

知道是否有类似$(“#panelC1”)的内容.fadeOut(“slow”)? 会是理想的。

(这是垂直布局的演示,而不是答案本身 )。

根据我如何理解你的问题, VerticalLayout可能是一个首选的解决方案……

在此处输入图像描述

 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.EventQueue; import java.awt.GridBagLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.LineBorder; import org.jdesktop.swingx.VerticalLayout; public class VerticalLayoutTest { public static void main(String[] args) { new VerticalLayoutTest(); } public VerticalLayoutTest() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { setLayout(new VerticalLayout()); JPanel outter = new JPanel(new BorderLayout()); outter.setBorder(new LineBorder(Color.BLACK)); outter.add(new JLabel("Outter 1"), BorderLayout.NORTH); JPanel inner = new JPanel(new GridBagLayout()); inner.setBackground(Color.GREEN); inner.add(new JLabel("Inner 1")); outter.add(inner); add(outter); inner.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { Component component = e.getComponent(); component.setVisible(false); component.invalidate(); component.validate(); revalidate(); repaint(); } }); add(Box.createVerticalGlue()); outter = new JPanel(new BorderLayout()); outter.setBorder(new LineBorder(Color.BLACK)); outter.add(new JLabel("Outter 1"), BorderLayout.NORTH); inner = new JPanel(new GridBagLayout()); inner.setBackground(Color.GREEN); inner.add(new JLabel("Inner 1")); outter.add(inner); add(outter); } } } 

调用panelC1.setVisible(false)使panelC1不可见,但它不会更改get[Preferred|Maximum|Minimum]Size方法定义的几何。 您可以

  • 覆盖定义方法,如如何使用BoxLayout:指定组件大小中所示

  • 从封闭的panelF1移除panelC1 ,如此处所示,并在再次可见时将其恢复。

  • 使用Box以外的布局。

如果我理解正确的话,你希望panelF1在其中的面板被隐藏时保持其大小。 有几种方法可以实现这一目标。 根据布局的设置方式,您可以在panelF1上调用setMinimumSize()并确保其最小大小略大于panelC1的首选大小。

但是,更灵活(也可能更合适)的方法是通过布局管理器本身。 阅读BoxLayout上的教程 ,如果它没有达到您想要的效果,请尝试使用其他布局管理器。 GroupLayoutGridBagLayout都非常灵活,但它们也很难管理。 我最近成了MigLayout的粉丝。

编辑

好吧,猜测我误解了你的问题,因为图片和文字的布局最终如何。 那实际上比我想象的要容易。 看看我链接的BoxLayout教程 – 我认为你正在寻找的是:

 PanelB.add(panelF1); PanelB.add(Box.createVerticalGlue()): PanelB.add(panelF2);