TextFieldWidget 不适用于 FabricMC (Java)

TextFieldWidget not working with FabricMC (Java)

最近我一直在尝试学习一些关于 Minecraft Modding 的东西。我选择使用 Fabric Mod Loader 到 make/test 我制作的模组。

为此,我正在做一个最近的项目(更像是测试),它与屏幕有关。关于这个问题已经足够了。

所以我制作了一个文本字段,但出于某种原因,当我键入时其中的文本没有显示..
问题视频: https://streamable.com/ik7j79
代码: https://pastebin.com/8XtSGpWj

package battledash2.clientcommands.screens;

import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.*;
import net.minecraft.util.Formatting;

import java.util.logging.LogManager;

public class GuiScreen extends Screen {
    public GuiScreen(Text title) {
        super(title);
    }

    protected void init() {
        // init

        System.out.println("Loading Screen");
    }

    public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
        matrices.push();
        renderBackground(matrices);

        /*
         * this.addButton(new ButtonWidget(this.width / 2 - 102, this.height / 4 + 24 + -16, 204, 20, new TranslatableText("menu.returnToGame"), (buttonWidgetx) -> {
         *          this.client.openScreen((Screen)null);
         *          this.client.mouse.lockCursor();
         *       }));
         */

        TextFieldWidget textfield = new TextFieldWidget(textRenderer, this.width / 2 - 102, this.height / 4 + 24 + -16, 204, 20, new LiteralText("Chat message").setStyle(Style.EMPTY.withColor(Formatting.WHITE)));

        this.addButton(textfield);

        ButtonWidget submit = new ButtonWidget(this.width / 2 - 102, (this.height / 4 + 24 + -16)+(this.height / 4 + 24 + -16)+10, 204, 20, new LiteralText("Send chat message"), button->{
            String message = textfield.getText();

            MinecraftClient.getInstance().openScreen((Screen) null);
            MinecraftClient.getInstance().mouse.lockCursor();

            MinecraftClient.getInstance().player.sendChatMessage(message);
        });
        this.addButton(submit);

        // drawTextWithShadow(matrices, textRenderer, new LiteralText("Chat message").setStyle(Style.EMPTY.withColor(Formatting.WHITE)), 10, 10, 0xFFFFFFFF);

        matrices.pop();
        super.render(matrices, mouseX, mouseY, delta);
    }
}

好的,我发现了问题。这是因为我将小部件放在“渲染”方法中。我在主方法中创建了一个名为“initWidgets”和 运行 initWidgets() 的新方法,然后将小部件移动到“initWidgets”方法并且它起作用了。