Java图形化编程之FlowLayout源码解析

FlowLayout将组件从左到右“流动"到窗体上,直到占满上方的空间,然后向下移动一行,继续流动。在FlowLayout中的组件都将被压缩到它们的最小尺寸,所以可能会得到令人惊讶的效果。那么FlowLayout内部的处理逻辑是如何做的呢,下图是它的几个核心方法:

FlowLayout核心处理逻辑

preferredLayoutSize

  • 这个方法在布局之前就会调用来确定大小尺寸.
public Dimension preferredLayoutSize(Container target) {
  synchronized (target.getTreeLock()) {
    //初始化一个大小为0子尺寸容器类来记录所有组件尺寸的总和
    Dimension dim = new Dimension(0, 0);
    //获取容器中的组件数量
    int nmembers = target.getComponentCount();
    //标记第一个组件是否可视
    boolean firstVisibleComponent = true;
    //基准线
    boolean useBaseline = getAlignOnBaseline();
    //上偏移量
    int maxAscent = 0;
    //下偏移量
    int maxDescent = 0;
    for (int i = 0; i < nmembers; i++) {
      //遍历组件
      Component m = target.getComponent(i);
      //是否可见
      if (m.isVisible()) {
        //获取组件尺寸
        Dimension d = m.getPreferredSize();
        //计算当前能容纳下所有组件的最大高度
        dim.height = Math.max(dim.height, d.height);
        if (firstVisibleComponent) {
          //如果是第一个可视组件width就不用加水平间隙hgap
          firstVisibleComponent = false;
        } else {
          //否则的话加上水平间隙hagp
          dim.width += hgap;
        }
        //容器的宽度增加一个组件的宽度
        dim.width += d.width;
        //基准线适配
        if (useBaseline) {
          //根据组件的宽高获取对应的基准线
          int baseline = m.getBaseline(d.width, d.height);
          //非负数情况
          if (baseline >= 0) {
            //记录向上的偏移量
            maxAscent = Math.max(maxAscent, baseline);
            //记录向下的偏移量
            maxDescent = Math.max(maxDescent, d.height - baseline);
          }
        }
      }
    }
    if (useBaseline) {
      //高度修正
      dim.height = Math.max(maxAscent + maxDescent, dim.height);
    }
    Insets insets = target.getInsets();
    //因为容器的边缘和组件之间会存在一个间隙,所以最后需要加上四周的间隙来最终确定容器的尺寸大小
    dim.width += insets.left + insets.right + hgap * 2;
    dim.height += insets.top + insets.bottom + vgap * 2;
    return dim;
  }
}

minimumLayoutSize

  • 这个方法用途是在计算布局所需的最小尺寸大小,但是在Debug过程中发现,无论是设置了容器大小还是未设置,这个方法均未调用。猜想很有可能这个方法和preferredLayoutSize方法在只会选择其一进行调用,因为它们的内部处理逻辑十分相似.
public Dimension minimumLayoutSize(Container target) {
  synchronized (target.getTreeLock()) {
    //判断是否使用了基准线
    boolean useBaseline = getAlignOnBaseline();
    //初始化一个大小为0子尺寸容器类来记录所有组件尺寸的总和
    Dimension dim = new Dimension(0, 0);
    //获取组件数量
    int nmembers = target.getComponentCount();
    //上偏移量
    int maxAscent = 0;
    //下偏移量
    int maxDescent = 0;
    //第一个可见组件
    boolean firstVisibleComponent = true;

    for (int i = 0; i < nmembers; i++) {
      //遍历组件
      Component m = target.getComponent(i);
      //是否可见
      if (m.visible) {
        //获取最小尺寸
        Dimension d = m.getMinimumSize();
        //开始记录行高
        dim.height = Math.max(dim.height, d.height);
        if (firstVisibleComponent) {
          firstVisibleComponent = false;
        } else {
          //记录水平间隙
          dim.width += hgap;
        }
        //记录宽度
        dim.width += d.width;
        if (useBaseline) {
          //如果使用了基准线的话需要对高度进行修正
          int baseline = m.getBaseline(d.width, d.height);
          if (baseline >= 0) {
            maxAscent = Math.max(maxAscent, baseline);
            maxDescent = Math.max(maxDescent,
                                  dim.height - baseline);
          }
        }
      }
    }
    //高度修正
    if (useBaseline) {
      dim.height = Math.max(maxAscent + maxDescent, dim.height);
    }
    Insets insets = target.getInsets();
    //因为容器的边缘和组件之间会存在一个间隙,所以最后需要加上四周的间隙来最终确定容器的尺寸大小
    dim.width += insets.left + insets.right + hgap * 2;
    dim.height += insets.top + insets.bottom + vgap * 2;
    return dim;
  }
}

layoutContainer

  • 这个方法和Android中的onLayout方法很相似,因为它也是在父类Container也是onLayout方法中调用的。
public void layoutContainer(Container target) {
  //获取锁对象
  synchronized (target.getTreeLock()) {
    //获取内间距对象
    Insets insets = target.getInsets();
    //计算组件的真实宽度(除去左右内间距和间隙)
    int maxwidth = target.width - (insets.left + insets.right + hgap * 2);
    //获取组件数量
    int nmembers = target.getComponentCount();
    //初始化坐标
    int x = 0, y = insets.top + vgap;
    int rowh = 0, start = 0;

    //获取组件的方向是否是LTR
    boolean ltr = target.getComponentOrientation().isLeftToRight();
    //基准线
    boolean useBaseline = getAlignOnBaseline();
    //上偏移量
    int[] ascent = null;
    //下偏移量
    int[] descent = null;

    //如果使用了基准线 那么就需要根据上下偏移量去修正位置
    if (useBaseline) {
      ascent = new int[nmembers];
      descent = new int[nmembers];
    }

    for (int i = 0; i < nmembers; i++) {
      //遍历容器中的组件
      Component m = target.getComponent(i);
      //可视化
      if (m.isVisible()) {
        //获取尺寸
        Dimension d = m.getPreferredSize();
        //设置宽高
        m.setSize(d.width, d.height);
        //基准线模式
        if (useBaseline) {
          //获取基准线
          int baseline = m.getBaseline(d.width, d.height);
          //非0情况
          if (baseline >= 0) {
            ascent[i] = baseline;
            descent[i] = d.height - baseline;
          } else {
            //基准线为负数
            ascent[i] = -1;
          }
        }
        //单行之间的水平坐标计算
        if ((x == 0) || ((x + d.width) <= maxwidth)) {
          if (x > 0) {
            //x>0说明当前组件不是第一个组件需要加上组件之间的水平间隙
            x += hgap;
          }
          //坐标向右偏移
          x += d.width;
          //水平坐标修正
          rowh = Math.max(rowh, d.height);
        } else {
          //换行
          rowh = moveComponents(target, insets.left + hgap, y,
                                maxwidth - x, rowh, start, i, ltr,
                                useBaseline, ascent, descent);
          x = d.width;
          y += vgap + rowh;
          rowh = d.height;
          start = i;
        }
      }
    }
    //组件的真正摆放的是在这个方法中去处理的
    moveComponents(target, insets.left + hgap, y, maxwidth - x, rowh,
                   start, nmembers, ltr, useBaseline, ascent, descent);
  }
}

moveComponents

/**
  * Centers the elements in the specified row, if there is any slack.
  * 将元素放到指定的行中
  *
  * @param target      the component which needs to be moved 需要被移动的组件
  * @param x           the x coordinate  x坐标
  * @param y           the y coordinate  y坐标
  * @param width       the width dimensions 宽
  * @param height      the height dimensions 高
  * @param rowStart    the beginning of the row   行始端
  * @param rowEnd      the the ending of the row  行末端
  * @param useBaseline Whether or not to align on baseline. 是否发对齐基准线
  * @param ascent      Ascent for the components. This is only valid if
  *                    useBaseline is true.
  *                    使组件向上偏移,这个只有在使用基准线的前提下有效
  * @param descent     Ascent for the components. This is only valid if
  *                    useBaseline is true.
  *                    使组件向下偏移,这个只有在使用记住县的前提下有效
  * @return actual row height 返回行高
  */
private int moveComponents(Container target, int x, int y, int width, int height,
                           int rowStart, int rowEnd, boolean ltr,
                           boolean useBaseline, int[] ascent,
                           int[] descent) {
  //根据newAlign方式去修正
  switch (newAlign) {
    case LEFT:
      //居左  LTR: X = X ; RTL: X = X + Width;
      x += ltr ? 0 : width;
      break;
    case CENTER:
      //居中
      x += width / 2;
      break;
    case RIGHT:
      //居右 LTR: X= X + width ; RTL: X = X;
      x += ltr ? width : 0;
      break;
    case LEADING:
      //沿着容器的左端
      break;
    case TRAILING:
      //沿着容器的后端
      x += width;
      break;
  }
  //上偏移量
  int maxAscent = 0;
  //非基准线高度
  int nonbaselineHeight = 0;
  //基准线偏移量
  int baselineOffset = 0;
  //使用了基准线的情况
  if (useBaseline) {
    int maxDescent = 0;
    for (int i = rowStart; i < rowEnd; i++) {
      //遍历一行的组件
      Component m = target.getComponent(i);
      //可视的情况下
      if (m.visible) {
        if (ascent[i] >= 0) {
          //记录上下偏移量
          maxAscent = Math.max(maxAscent, ascent[i]);
          maxDescent = Math.max(maxDescent, descent[i]);
        } else {
          //高度修正
          nonbaselineHeight = Math.max(m.getHeight(),
                                       nonbaselineHeight);
        }
      }
    }
    //高度修正
    height = Math.max(maxAscent + maxDescent, nonbaselineHeight);
    //基准线偏移量修正
    baselineOffset = (height - maxAscent - maxDescent) / 2;
  }
  for (int i = rowStart; i < rowEnd; i++) {
    //遍历一行的组件
    Component m = target.getComponent(i);
    //可视的情况下
    if (m.isVisible()) {
      //修正过后的y坐标值
      int cy;
      if (useBaseline && ascent[i] >= 0) {
        //如果使用了基准线并且对应的向上偏移量非负数
        cy = y + baselineOffset + maxAscent - ascent[i];
      } else {
        cy = y + (height - m.height) / 2;
      }
      //对当前的布局模式进行判断并设置组件的位置
      if (ltr) {
        m.setLocation(x, cy);
      } else {
        m.setLocation(target.width - x - m.width, cy);
      }
      //设置完以后x坐标向右偏移
      x += m.width + hgap;
    }
  }
  return height;
}

注:关于Insets类,它是一个容器边框的表示,它指定容器必须在每个边缘留下空间(可以为0),空间可以是边框,空格或标题。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,392评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,258评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,417评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,992评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,930评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,199评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,652评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,327评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,463评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,382评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,432评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,118评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,704评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,787评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,999评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,476评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,057评论 2 341

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,282评论 25 707
  • GridLayout将一个容器的组件放在一个矩形网格中。容器被分成等大小的矩形,每个矩形中放置一个组件。那么Gri...
    旅旅人阅读 1,121评论 0 1
  • BorderLayout是一个限制性布局,它只允许在东、南、西、北和中心五大区域内去放置组件,每个区域至多一个组件...
    旅旅人阅读 537评论 0 0
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,722评论 1 92
  • 如望晨风携露有悲凉,何不画天踏日傲风扬
    艾薇琪阅读 281评论 0 0