मुख्य सामग्री पर जाएँ
CrossFly 100% ओपन सोर्सGitHub पर फ़ॉलो करें
CrossFlyCrossFly
शुरू करें
सभी उदाहरण
रेसिपीफ़ॉर्मइनपुट84 पंक्तियाँ · 1 फ़ाइलें

A form that fights back

setMode does most of the work: it picks the keyboard, the mask and the validation together. A regex on the way out only tells the user they already got it wrong.

यह क्या दिखाता है

A form that prevents mistakes instead of complaining about them afterwards.

अपनी मशीन पर चलाएँ

हर उदाहरण एक साधारण Window है, इसलिए लॉन्चर को सिर्फ़ उसका नाम चाहिए।

gradlew lwjgl3:run --args="classname dev.cf.recipe.R10Form"
इसे बदलना

Java पहले से संकलित होता है, इसलिए ब्राउज़र आपके बदलाव को खुद दोबारा नहीं बना सकता — यहाँ दिखा कोड प्रकाशित होने से पहले TeaVM द्वारा JavaScript में बदला गया था। फ़ाइल को किसी प्रोजेक्ट में कॉपी कीजिए और वह चारों प्लेटफ़ॉर्म पर बिना बदलाव चलती है; ऊपर दी गई कमांड ही पूरी तैयारी है।

यहीं चल रहा हैCrossFly 0.1.32
ऐप शुरू हो रहा है…

TeaVM ने इसे JavaScript में संकलित किया — यह वही कोड है, उसका वीडियो नहीं। · सभी उदाहरण

यह क्या इस्तेमाल करता है

इस उदाहरण में छुए गए फ़्रेमवर्क टाइप। जिनमें लिंक है उनका संदर्भ पृष्ठ मौजूद है।

ButtonComponentHintLabelScreenTextFieldTextFieldStyleWidgets

सोर्स कोड

पूरा, बिना कटौती के। यह वही फ़ाइल है, उसका अंश नहीं।

कच्चा
dev/cf/recipe/R10Form.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.*;

/**
 * A form that prevents mistakes instead of complaining about them afterwards.
 */
public class R10Form extends Recipe
{
   private TextField amount, when, pin;
   private Label problem;

   public String title()
   {
      return "A form that fights back";
   }

   public String note()
   {
      return "setMode does most of the work: it picks the keyboard, the mask and the validation"
         + " together. A regex on the way out only tells the user they already got it wrong.";
   }

   public void build(Component body) throws Exception
   {
      // CURRENCY masks as you type and gives a numeric keyboard on a phone. Two decimals is the
      // mask's business, not the screen's — no parsing here, no "please use a dot".
      amount = field(body, "Amount", EditMode.CURRENCY);

      // DATE opens the date picker rather than a text keyboard, so an impossible date cannot be
      // typed at all. Same for TIME.
      when = field(body, "Date", EditMode.DATE);

      pin = field(body, "PIN", EditMode.PASSWORD);
      // maxLength lives on the STYLE, not on the field — along with the mask, the valid characters
      // and the keyboard. Worth knowing before you write your own length check in a key handler.
      TextFieldStyle st = pin.getStyle();
      st.maxLength(6);

      // The message is the one thing on this screen that means something went wrong, so it is the
      // one thing wearing the error tone. In secondary grey it read as another caption.
      problem = label("", 1f, true, Tones.danger(), Position.START);
      problem.setVisible(false);
      body.add(LAY_AUTO, outgap(problem, 0, .4f, 0, .4f));

      Button ok = Tones.action("Submit");
      ok.testTag("form.submit");
      ok.onAction(c -> submit());
      body.add(LAY_AUTO, ok);
   }

   private TextField field(Component body, String caption, EditMode mode) throws Exception
   {
      TextField f = new TextField();
      f.caption(caption);
      f.setMode(mode);
      f.testTag("form." + caption.toLowerCase());
      body.add(LAY_AUTO, outgap(f, 0, 0, 0, .7f));
      return f;
   }

   /** Says WHICH field and WHY. "Please complete all fields" is what a form says when nobody owns the rule. */
   private void submit()
   {
      String p = amount.getText().isEmpty() ? "How much? The amount is still empty."
         : when.getText().isEmpty() ? "Pick a date — tap the field and the picker opens."
            : pin.getText().length() < 4 ? "The PIN is at least four digits." : null;
      problem.setText(p == null ? "" : p);
      problem.setVisible(p != null);
      problem.setValidLayout(false);
      if (p == null)
         Hint.show("Submitted", 2000);
      Screen.repaint();
   }
}
एआई सहायक के लिए

इस पृष्ठ के मशीन-पठनीय रूप, ताकि मॉडल API का अनुमान लगाने के बजाय उदाहरण पढ़ सके।

यह उदाहरण, JSON मेंयह उदाहरण, Markdown में
पिछलाMaking a screen testableअगलाTelling the user something