0

最近我一直在尝试学习一些关于 Minecraft Modding 的东西。我选择使用Fabric Mod Loader来制作/测试我正在制作的 mod。

在这样做的过程中,我正在处理一个最近的项目(更像是测试),它与屏幕有关。足够了,关于这个问题。

所以我创建了一个文本字段,由于某种原因,当我键入时,其中的文本没有显示出来。
问题的视频: 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);
    }
}
4

1 回答 1

0

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

于 2021-01-21T14:17:12.400 回答