栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何让JToolBars包装到下一行(FlowLayout),而又不隐藏在它们下面的JPanel中呢?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何让JToolBars包装到下一行(FlowLayout),而又不隐藏在它们下面的JPanel中呢?

我以前遇到过这个问题。我发现最好的解决方案是使用

FlowLayout
考虑到垂直更改的修改版本,并将其包装到下一行。这是这种布局的代码。

import java.awt.*; 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 rowx = 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);          }       }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483435.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号