简体   繁体   中英

resizable layout swing when disappears JPanel

I have many jPanel (panelF) that are added in other jPanel(panelB). jPanelF contain jPanelC. 在此输入图像描述

I have set the layout to add them as follows:

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

I set visible false in jPanelC1. But JPanelF2 preserves distance and not want to make that blank space

在此输入图像描述 What I want is that when disappearing JPanelC1. Distance is maintained between the jPanelF and not be a white space. I tried validate() and change the layout but I fail. What would be the way? Thank you very much. Sorry for my English

know if there are something like $("#panelC1").fadeOut("slow")? would be ideal.

Invoking panelC1.setVisible(false) makes panelC1 invisible, but it doesn't change the geometry defined by the get[Preferred|Maximum|Minimum]Size methods. You can

(This is a demonstration of vertical layout, not an answer per se ).

Based on how I understand you problem, VerticalLayout may be a preferred solution...

在此输入图像描述

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);

        }
    }
}

If I understood you correctly, you want panelF1 to maintain its size when the panel inside it is hidden. There are several ways to accomplish this. Depending on how your layout is set up, you could call setMinimumSize() on panelF1 and make sure its minimum size is slightly larger than panelC1's preferred size.

A more flexible (and likely more appropriate) way to do this, though, is via the layout manager itself. Read the tutorial on BoxLayout , and if it doesn't do what you want it to, try using a different layout manager. Both GroupLayout and GridBagLayout are very flexible, but they can also be difficult to manage. I've recently become somewhat of a fan of MigLayout .

EDIT

OK, guess I misunderstood your question due to how the layout of the pictures and text ended up. It's actually easier than I thought, then. Look at the BoxLayout tutorial I linked - what I think you're looking for is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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