跳到内容
CrossFly 是 100% 开源在 GitHub 关注我们
CrossFlyCrossFly
开始使用
全部示例
配方布局80 行 · 1 个文件

Cards that reflow

Tell CardGrid the width a card wants, not how many columns to use. Resize the window — the reflow is the framework's, not yours.

它展示了什么

Cards that reflow, and the width check you do not write.

Resize the window: one column becomes two, then three. There is no listener here, no if (width > 600), and no phone/tablet branch. CardGrid is told the width a card wants and the column range it may use, and it works the rest out — which means the same code is right on a phone, a tablet, a split-screen window and a desktop nobody tested on.

sizeClass() exists for the decisions a layout genuinely cannot make: whether to show a second pane at all, whether a table is worth offering. Reach for it there, and not for spacing.

在你的机器上运行

每个示例都是普通的 Window,启动器只需要它的名字。

gradlew lwjgl3:run --args="classname dev.cf.recipe.R14Reflow"
修改它

Java 是提前编译的,浏览器无法自行重建你的修改 —— 这里的代码在发布前已由 TeaVM 转成 JavaScript。把文件复制到项目里,它在四个平台上原样运行;上面那条命令就是全部准备工作。

正在此处运行CrossFly 0.1.32
正在启动应用…

由 TeaVM 编译为 JavaScript —— 是同一份代码,不是录像。 · 全部示例

它用到了什么

本示例触及的框架类型。带链接的有参考页。

CardCardGridComponentLabelScreenStylesWidgets

源码

完整无删减。这就是那个文件,不是片段。

原始
dev/cf/recipe/R14Reflow.java
package dev.cf.recipe;

import dev.cf.Tones;

import dev.crossfly.ui.*;
import dev.crossfly.ui.color.*;
import dev.crossfly.ui.graphics.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.style.*;

import static dev.crossfly.ui.Widgets.*;

/**
 * Cards that reflow, and the width check you do not write.
 *
 * <p>Resize the window: one column becomes two, then three. There is no listener here, no
 * {@code if (width > 600)}, and no phone/tablet branch. {@link CardGrid} is told the width a card
 * <i>wants</i> and the column range it may use, and it works the rest out — which means the same
 * code is right on a phone, a tablet, a split-screen window and a desktop nobody tested on.</p>
 *
 * <p>{@link Screen#sizeClass()} exists for the decisions a layout genuinely cannot make: whether to
 * show a second pane at all, whether a table is worth offering. Reach for it there, and not for
 * spacing.</p>
 */
public class R14Reflow extends Recipe
{
   private static final String[] NAME = { "Bearings", "Fasteners", "Seals", "Hydraulics",
      "Electrical", "Tooling", "Adhesives", "Abrasives" };
   private static final int[] COUNT = { 128, 412, 96, 55, 210, 74, 31, 88 };

   private Label sizeLine;

   public String title()
   {
      return "Cards that reflow";
   }

   public String note()
   {
      return "Tell CardGrid the width a card wants, not how many columns to use. Resize the window"
         + " — the reflow is the framework's, not yours.";
   }

   public void build(Component body) throws Exception
   {
      sizeLine = label("", 1f, false, Styles.captionColor, Position.START);
      body.add(LAY_AUTO, outgap(sizeLine, 0, 0, 0, .8f));
      updateSizeLine();
      // Re-read on restyle: a resize repositions, which restyles, so this line follows the window
      // without a resize listener of its own.
      onRestyle(this::updateSizeLine);

      CardGrid grid = new CardGrid().columns(1, 3)
         .cardWidth(13f)
         .gap(.6f);
      grid.testTag("recipe.reflow");
      for (int i = 0; i < NAME.length; i++)
         grid.card(tile(NAME[i], COUNT[i], i));
      body.add(LAY_AUTO, grid);
   }

   private Component tile(String name, int count, int i) throws Exception
   {
      // A grid of cards is exactly where a rotating tone earns its keep: the reflow is the thing
      // being demonstrated, and identical grey tiles make it hard to see that anything moved.
      Card c = Tones.card(Tones.cycle(i));
      c.add(LAY_AUTO, label(name, 1.2f, true, Styles.textColor, Position.START));
      c.add(LAY_AUTO, outgap(label(count + " in stock", 1f, true, Tones.cycle(i),
         Position.START), 0, .2f, 0, 0));
      return c;
   }

   private void updateSizeLine()
   {
      sizeLine.setText("This window is " + Screen.sizeClass() + " — the grid decided the columns,"
         + " nothing here did.");
      sizeLine.setValidLayout(false);
   }
}
给 AI 助手

本页的机器可读版本,让模型直接读取示例集,而不是猜测 API。

本示例(JSON)本示例(Markdown)
上一个Waiting下一个A header that collapses