2010-09-09 67 views
18

我正在製作一個GUI,其中顯示了一個大的JPanel上方的三個JToolBar。這些工具欄總的來說非常大,所以我使用FlowLayout來使它們在到達JFrame邊界時換行到下一行。問題是,當他們換行到下一行時,它們被下面的JPanel隱藏起來..我希望我可以強制包含工具欄的JPanel足夠長,以顯示所有工具欄..如何讓JToolBar在下一行(FlowLayout)中不隱藏下面的JPanel?

有沒有辦法這個?或者有另一種方法可以使這些工具欄可見嗎?

回答

25

我以前遇到過這個問題。我發現最好的解決方案是使用FlowLayout的修改版本,該版本考慮垂直變化並將它們包裝到下一行。這是這種佈局的代碼。

import java.awt.*; 

/** 
    * A modified version of FlowLayout that allows containers using this 
    * Layout to behave in a reasonable manner when placed inside a 
    * JScrollPane 
    * @author Babu Kalakrishnan 
    * Modifications by greearb and jzd 
    */ 

public class ModifiedFlowLayout extends FlowLayout { 
     public ModifiedFlowLayout() { 
       super(); 
      } 

      public ModifiedFlowLayout(int align) { 
       super(align); 
      } 
     public ModifiedFlowLayout(int align, int hgap, int vgap) { 
      super(align, hgap, vgap); 
     } 

     public Dimension minimumLayoutSize(Container target) { 
      // Size of largest component, so we can resize it in 
      // either direction with something like a split-pane. 
      return computeMinSize(target); 
     } 

     public Dimension preferredLayoutSize(Container target) { 
      return computeSize(target); 
     } 

     private Dimension computeSize(Container target) { 
      synchronized (target.getTreeLock()) { 
      int hgap = getHgap(); 
      int vgap = getVgap(); 
      int w = target.getWidth(); 

      // Let this behave like a regular FlowLayout (single row) 
      // if the container hasn't been assigned any size yet 
      if (w == 0) { 
       w = Integer.MAX_VALUE; 
      } 

      Insets insets = target.getInsets(); 
      if (insets == null){ 
       insets = new Insets(0, 0, 0, 0); 
      } 
      int reqdWidth = 0; 

      int maxwidth = w - (insets.left + insets.right + hgap * 2); 
      int n = target.getComponentCount(); 
      int x = 0; 
      int y = insets.top + vgap; // FlowLayout starts by adding vgap, so do that here too. 
      int rowHeight = 0; 

      for (int i = 0; i < n; i++) { 
       Component c = target.getComponent(i); 
       if (c.isVisible()) { 
        Dimension d = c.getPreferredSize(); 
        if ((x == 0) || ((x + d.width) <= maxwidth)) { 
         // fits in current row. 
         if (x > 0) { 
         x += hgap; 
         } 
         x += d.width; 
         rowHeight = Math.max(rowHeight, d.height); 
        } 
        else { 
         // Start of new row 
         x = d.width; 
         y += vgap + rowHeight; 
         rowHeight = d.height; 
        } 
        reqdWidth = Math.max(reqdWidth, x); 
       } 
      } 
      y += rowHeight; 
      y += insets.bottom; 
      return new Dimension(reqdWidth+insets.left+insets.right, y); 
      } 
     } 

     private Dimension computeMinSize(Container target) { 
      synchronized (target.getTreeLock()) { 
      int minx = Integer.MAX_VALUE; 
      int miny = Integer.MIN_VALUE; 
      boolean found_one = false; 
      int n = target.getComponentCount(); 

      for (int i = 0; i < n; i++) { 
       Component c = target.getComponent(i); 
       if (c.isVisible()) { 
        found_one = true; 
        Dimension d = c.getPreferredSize(); 
        minx = Math.min(minx, d.width); 
        miny = Math.min(miny, d.height); 
       } 
      } 
      if (found_one) { 
       return new Dimension(minx, miny); 
      } 
      return new Dimension(0, 0); 
      } 
     } 

    } 
+0

什麼是'synchronized(target.getTreeLock())'用於? – 2011-06-13 19:16:57

+0

@Jason保持計算線程安全。但是,我不知道是否有必要。我對此的修改從不處理同步。 – jzd 2011-06-13 19:21:28

+0

謝謝你!這爲我節省了很多時間,乾淨的解決方案也是 – 2011-12-02 08:56:02

相關問題