Saltar al contenido
CrossFly es 100% open sourceSíguenos en GitHub
CrossFlyCrossFly
Empezar
Todos los ejemplos
RecetasDisposición93 líneas · 1 archivos

Layouts

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

Qué muestra esto

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.

Ejecutarlo en tu máquina

Cada ejemplo es una Window normal, así que el lanzador solo necesita su nombre.

gradlew lwjgl3:run --args="classname dev.cf.recipe.R01Layouts"
Editarlo

Java se compila antes de ejecutarse, así que el navegador no puede reconstruir una edición por su cuenta: el código que ves aquí se convirtió en JavaScript con TeaVM antes de publicarse. Copia el archivo a un proyecto y funciona sin cambios en las cuatro plataformas; el comando de arriba es toda la preparación.

Ejecutándose aquíCrossFly 0.1.32
Iniciando la aplicación…

Compilado a JavaScript por TeaVM: es el mismo código, no un vídeo de él. · Todos los ejemplos

Qué usa

Tipos del framework que toca este ejemplo. Los enlazados tienen página de referencia.

CardComponentGapGridLayoutHorizontalLayoutLabelLayoutPalColorVerticalLayoutWidgets

El código

Completo, sin recortes. Es el archivo, no un fragmento.

Crudo
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);
   }
}
Para una IA

Versiones de esta página legibles por máquina, para que un modelo lea el estante en vez de adivinar la API.

Este ejemplo, en JSONEste ejemplo, en Markdown
SiguienteTheme tokens