2010-11-16 165 views
0

我有一個JPanel,我在裏面動態地創建了JCheckBoxes。 這些必須添加JCheckBoxes總是並排。如果在側面插入更多空間,則會創建一個新的JCheckBoxes行,就像在簡單的文本編輯器中一樣。JScrollPane中的JPanel

這是完美的發生。但...

我將此JPanel的佈局設置爲FlowLayout,正是我想要的。

顯而易見的問題是窗口空間有限。因此,一個很好的解決方案是:將此JPanel插入到JScrollPane中,並僅在垂直滾動中進行。 但我有問題。雖然您只能製作一個垂直滾動條,但這些項目總是並排添加「永遠」。垂直滾動只是水平不起作用。

我已經嘗試了很多方法來使滾動垂直,但沒有任何工作(如果它已經工作,我不會在這裏:])。

那麼,有沒有人有類似的問題,並可以幫助我?

我將非常感謝那些幫助我的人。

沒有更多。

+2

這意味壞UI設計 - JScrollPane中通常用來提供一個視圖到表或圖像,而不是控制面板(畢竟,有多少次你見過在Windows,Mac系統等這種方法)?更好的方法是使用CardLayout將多個選項卡或JPanel堆疊在一起。 – Adamski 2010-11-16 21:00:24

回答

4

我處理了與ScrollPanes和FlowLayouts相同的問題。我發現最好的解決方案是使用FlowLayout的修改版本,該版本考慮垂直變化。這是這種佈局的代碼。您可以將它包含在您的項目中,並將其稱爲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

「@作者Babu Kalakrishnan - 修改..」啊是的。其中一個usenet的GUI大師(好吧,無論如何我都知道他們在哪裏)。 ;) – 2011-03-18 15:19:41