【问题标题】:How can I let JToolBars wrap to the next line (FlowLayout) without them being hidden ty the JPanel below them?如何让 JToolBars 换行到下一行(FlowLayout)而不会将它们隐藏在它们下方的 JPanel 中?
【发布时间】:2011-04-10 10:37:06
【问题描述】:

我正在制作一个在大 JPanel 上方显示三个 JToolBar 的 GUI。这些工具栏总体上非常大,所以我使用 FlowLayout 让它们在到达 JFrame 边框时换行到下一行。问题是,当它们换行到下一行时,它们会被下面的 JPanel 隐藏。我希望我可以强制包含工具栏的 JPanel 增长到足以显示所有工具栏..

有没有办法做到这一点?还是有其他方法可以让这些工具栏可见?

【问题讨论】:

    标签: java swing jtoolbar flowlayout


    【解决方案1】:

    看看WrapLayout。它对我有用。 Here 是代码。

    【讨论】:

      【解决方案2】:

      我以前遇到过这个问题。我发现最好的解决方案是使用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);
                }
             }
      
          }
      

      【讨论】:

      • synchronized (target.getTreeLock()) 是干什么用的?
      • @Jason 保持计算线程安全。但是,我不知道这是否有必要。我对此的修改从未涉及同步。
      • 将 miny 初始化为 Integer.MIN_VALUE 似乎很可疑。应该是 Integer.MAX_VALUE,就像 minx 一样?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 2020-01-29
      • 2014-06-07
      • 1970-01-01
      相关资源
      最近更新 更多