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

Switching skins

Shape, radius, padding and typeface all come from the skin. Nothing below knows which one is in force.

它展示了什么

The same screen under Material 3, Fluent and iOS — with no per-skin code anywhere.

在你的机器上运行

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

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

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

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

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

它用到了什么

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

ButtonCardComponentDebugFluentSkinHorizontalLayoutIOSSkinMaterialSkinScreenSkinStylesSwitchTextFieldWidgets

源码

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

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

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

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

/**
 * The same screen under Material 3, Fluent and iOS — with no per-skin code anywhere.
 */
public class R03Skins extends Recipe
{
   public String title()
   {
      return "Switching skins";
   }

   public String note()
   {
      return "Shape, radius, padding and typeface all come from the skin. Nothing below knows which"
         + " one is in force.";
   }

   public void build(Component body) throws Exception
   {
      Component linha = new Component();
      linha.setLayout(new HorizontalLayout(Position.START, Position.CENTER));
      // One per call: the add with several weights belongs to GridLayout (see R01Layouts).
      linha.add(LAY_AUTO, outgap(botao("Material", new MaterialSkin()), 0, 0, .5f, 0));
      linha.add(LAY_AUTO, outgap(botao("Fluent", new FluentSkin()), 0, 0, .5f, 0));
      linha.add(LAY_AUTO, botao("iOS", new IOSSkin()));
      body.add(LAY_AUTO, outgap(linha, 0, 0, 0, 1f));

      // Pieces whose shape comes entirely from the skin — that is where the switch shows.
      body.add(LAY_AUTO, outgap(new TextField().caption("A field"), 0, 0, 0, .6f));
      body.add(LAY_AUTO, outgap(new Button("A button"), 0, 0, 0, .6f));
      body.add(LAY_AUTO, outgap(new Switch("A switch"), 0, 0, 0, .6f));
      Card cartao = new Card();
      cartao.add(LAY_AUTO, label("A card", 1.2f, true, Styles.textColor, Position.START));
      body.add(LAY_AUTO, cartao);
   }

   /**
    * {@code apply(skin, onApplied)}, never {@code apply(skin)}.
    *
    * <p>On the web the font atlas of a skin the app did not start with is downloaded on demand, and
    * only the callback form waits for it. On native builds the callback runs before the method
    * returns, so this one line is right on every platform.</p>
    */
   private Button botao(String rotulo, Skin skin)
   {
      Button b = new Button(rotulo);
      b.onAction(c -> Skin.apply(skin, this::recriar));
      return b;
   }

   /** Switching skins is the app's decision, so the rebuild is here — never inside the rebuild itself. */
   private void recriar()
   {
      try
      {
         removeAll();
         onAdd(false);
         setValidLayout(false);
         Screen.repaint();
      }
      catch (Exception e)
      {
         dev.crossfly.util.Debug.handle(e);
      }
   }
}
给 AI 助手

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

本示例(JSON)本示例(Markdown)
上一个Theme tokens下一个Bottom sheet with a fixed footer