2010-09-09 13 views
18

Tôi đang trong quá trình tạo GUI cho thấy ba JToolBars phía trên một JPanel lớn. Những thanh công cụ này là rất lớn, vì vậy tôi đang sử dụng FlowLayout để làm cho chúng được bọc vào dòng tiếp theo nếu chúng đạt đến biên giới JFrame. Vấn đề là khi họ quấn vào dòng tiếp theo, họ trở nên ẩn bởi JPanel bên dưới .. Tôi ước tôi có thể ép JPanel chứa các thanh công cụ đủ lớn để hiển thị tất cả các thanh công cụ.Làm thế nào tôi có thể để JToolBars quấn vào dòng tiếp theo (FlowLayout) mà không bị ẩn đi bởi JPanel bên dưới chúng?

Có cách nào để làm điều này? Hoặc có cách nào khác để làm cho các thanh công cụ này có thể nhìn thấy được không?

Trả lời

25

Tôi đã gặp sự cố này trước đây. Tôi tìm thấy giải pháp tốt nhất là sử dụng phiên bản sửa đổi của FlowLayout có tính đến các thay đổi theo chiều dọc và kết thúc chúng vào dòng tiếp theo. Đây là mã cho bố cục như vậy.

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

'đồng bộ hóa (target.getTreeLock())' được sử dụng để làm gì? –

+0

@ Jason Để giữ cho các tính toán an toàn chỉ. Tuy nhiên, tôi không biết liệu nó có cần thiết không. Sửa đổi của tôi cho điều này không bao giờ được xử lý với đồng bộ hóa. – jzd

+0

cảm ơn người đàn ông !! đã tiết kiệm cho tôi rất nhiều thời gian, giải pháp sạch sẽ quá –

Các vấn đề liên quan