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

Layouts

Any Component becomes a container with setLayout. The same three labels, three ways.

它展示了什么

The three layouts you will use for almost everything, and what each does with the same content.

One thing worth knowing before you copy this: the multi-child form of add - several weights and several components in one call - belongs to GridLayout alone. Vertical and horizontal layouts take one child per call and throw a UIException otherwise, at show time rather than at compile time.

在你的机器上运行

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

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

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

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

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

它用到了什么

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

CardComponentGapGridLayoutHorizontalLayoutLabelLayoutPalColorVerticalLayoutWidgets

源码

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

原始
dev/cf/recipe/R01Layouts.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.*;

/**
 * The three layouts you will use for almost everything, and what each does with the same content.
 *
 * <p>One thing worth knowing before you copy this: the multi-child form of {@code add} - several
 * weights and several components in one call - belongs to {@link GridLayout} alone. Vertical and
 * horizontal layouts take one child per call and throw a {@code UIException} otherwise, at show
 * time rather than at compile time.</p>
 */
public class R01Layouts extends Recipe
{
   private static final int IC_STACK = 0xE949, IC_ROW = 0xE947, IC_GRID = 0xE871;

   public String title()
   {
      return "Layouts";
   }

   public String note()
   {
      return "Any Component becomes a container with setLayout. The same three labels, three ways.";
   }

   public void build(Component body) throws Exception
   {
      // Each layout gets its own tone, and the three chips inside it are drawn in that tone. The
      // colour is doing the explaining: the same three chips appear three times, and what changed
      // between the panels is only where the layout put them.
      body.add(LAY_AUTO, caixa(IC_STACK, "VerticalLayout — stacked", Tones.accent(),
         new VerticalLayout(Position.START, Position.START)));

      // HorizontalLayout: side by side. The container is as wide as the SUM.
      body.add(LAY_AUTO, caixa(IC_ROW, "HorizontalLayout — in a row", Tones.support(),
         new HorizontalLayout(Position.START, Position.CENTER)));

      // GridLayout: you declare the column weights in add(). 1f is "take what is left".
      Component grade = new Component();
      grade.setLayout(new GridLayout(Position.START, Position.CENTER));
      grade.add(.25f, .5f, .25f, texto("25%", Tones.highlight()), texto("50%", Tones.highlight()),
         texto("25%", Tones.highlight()));
      body.add(LAY_AUTO, rotulado(IC_GRID, "GridLayout — declared weights", Tones.highlight(),
         grade));
   }

   /** Three chips under the given layout, inside a panel of that layout's tone. */
   private Component caixa(int icone, String rotulo, PalColor tom, Layout l) throws Exception
   {
      Component c = new Component();
      c.setLayout(l);
      // One child per call: the form with several weights at once belongs to GridLayout, and only to it.
      c.add(LAY_AUTO, texto("one", tom));
      c.add(LAY_AUTO, texto("two", tom));
      c.add(LAY_AUTO, texto("three", tom));
      return rotulado(icone, rotulo, tom, c);
   }

   private Component rotulado(int icone, String rotulo, PalColor tom, Component alvo)
      throws Exception
   {
      Card fora = Tones.card(tom);
      fora.setLayout(new VerticalLayout(Position.START, Position.START));
      fora.getStyle()
         .outgap(Gap.ofFont(0, 0, 0, .7f));
      fora.add(LAY_AUTO, Tones.head(icone, rotulo, tom));
      fora.add(LAY_AUTO, alvo);
      return fora;
   }

   /**
    * One item, in its panel's tone.
    *
    * <p>A plain toned label rather than a pill: a Badge added to a VerticalLayout is handed the
    * container's width, and forcing it to hug squeezed the text down to an unreadable smear. The
    * panel already carries the tone — the item only has to be legible and left-aligned, so that
    * WHERE the layout put it is the only thing that changes between the three panels.</p>
    */
   private Label texto(String t, PalColor tom)
   {
      return outgap(label(t, 1.1f, true, tom, Position.START), 0, .1f, .9f, .1f);
   }
}
给 AI 助手

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

本示例(JSON)本示例(Markdown)
下一个Theme tokens