当JPanel消失时,可调整大小的布局摆动 [英] resizable layout swing when disappears JPanel

查看:162
本文介绍了当JPanel消失时,可调整大小的布局摆动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他jPanel(panelB)中添加了许多jPanel(panelF)。
jPanelF包含jPanelC。

I have many jPanel (panelF) that are added in other jPanel(panelB). jPanelF contain jPanelC.

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

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

我在jPanelC1中设置了可见的false。但JPanelF2保留距离而不想留出空白

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


我想要的是在消失JPanelC1时。 jPanelF之间保持距离而不是空白区域。
我尝试了validate()并更改了布局,但是我失败了。会是什么样的?
非常感谢你。抱歉我的英语

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

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

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

推荐答案

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

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

根据我的理解你的问题, VerticalLayout 可能是首选解决方案......

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

        }
    }
}

这篇关于当JPanel消失时,可调整大小的布局摆动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆