2013-10-02 47 views
4

我在其他jPanel(panelB)中添加了許多jPanel(panelF)。 jPanelF包含jPanelC。 enter image description here消失時可調整大小的佈局擺動JPanel

我已經設置的佈局將它們加入到如下:

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

我設置jPanelC1可見假。但JPanelF2保留距離,不想讓空白空間

enter image description here 我想要的是,當消失JPanelC1。 jPanelF之間的距離保持不變,並且不成爲白色空間。 我嘗試驗證()並更改佈局,但我失敗了。這將是什麼方式? 非常感謝。對不起,我的英語

知道是否有像$(「#panelC1」)。fadeOut(「slow」)?將是理想的。

+0

我覺得這是BoxLayout'如何'工程... – MadProgrammer

+1

就個人而言,我會嘗試使用'VerticalLayout'從SwingX – MadProgrammer

+0

這個佈局應該使用呢?我想使用一個列表,因爲沒有任何佈局 – user60108

回答

3

這是垂直佈局的示範,而不是aswer每說

基於我如何理解你的問題,VerticalLayout可能是首選的解決方案...

enter image description here

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

     } 
    } 
} 
+1

非常感謝您的時間。我爲[sscce](http://sscce.org/)_and_動畫GIF設法爲 – user60108

+0

+1! – trashgod

0

如果我正確理解你,你希望panelF1在其內部的面板被隱藏時保持其大小。有幾種方法可以實現這一點。根據佈局設置的方式,您可以在panelF1上調用setMinimumSize(),並確保其最小尺寸略大於panelC1的首選尺寸。

儘管如此,更靈活(也可能更合適)的方法是通過佈局管理器本身。閱讀tutorial on BoxLayout,如果它沒有做到你想要的,嘗試使用不同的佈局管理器。 GroupLayoutGridBagLayout都非常靈活,但它們也可能難以管理。我最近變得有點迷戀MigLayout

編輯

OK,你猜我誤解你的問題,由於圖片和文字的佈局是如何結束。那實際上比我想象的要容易得多。看看BoxLayout的教程我聯繫 - 我想你要找的是:

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

不,我想減少JPanelF1和JPanelF1之間的距離。非常感謝 – user60108