java - 创建自定义 TextButton 类的问题

标签 java eclipse libgdx

免责声明: 我是 LibGDX 的新手,对 java 有相当基本的了解。我已经使用 Google 和 Stackoverflow 来寻找答案,但没有结果。抱歉,我的英语不是母语。

这是我的问题:

我想为游戏创建多个按钮,为了节省代码,我尝试创建一个自定义 TextButton 类。但是,我遇到了问题。

这是我到目前为止的代码:

package com.mygame.game;

import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;

public class customButton extends TextButton{

    TextButtonStyle customButtonStyle;

    BitmapFont font;
    TextureAtlas buttonAtlas;

    public customButton(String text, Skin skin, int x, int y) {
        super(text, skin);

        customButtonStyle = new TextButtonStyle();
        customButtonStyle.font = font;
        customButtonStyle.up = skin.getDrawable("accept-button");

        this.setStyle(customButtonStyle);
        this.setPosition(x, y);
    }

}

我用以下代码调用它:

customButton testlol;

...

skin = new Skin();
buttonAtlas = new TextureAtlas(Gdx.files.internal("icons"));
skin.addRegions(buttonAtlas);

testlol = new customButton("Testhallo", skin, 200, 200);
stage.addActor(testlol);

等等。但是,我收到错误:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: No com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle registered with name: default

...

at com.mygame.game.customButton.<init>(customButton.java:17)

错误引用了这一行:

        super(text, skin);

(我删除了一些多余的错误文本)

我看不出哪里出了问题。我的(这里的根本错误是什么?:/

最佳答案

您调用的 super 构造函数 super(text, Skin); 尝试使用默认样式实例化 TextButton。因此,您的皮肤必须已经为 TextButtons 定义了默认样式。或者,您可以从父类(super class)的构造函数中复制并粘贴代码而不是调用它,并根据需要进行调整以跳过尝试应用默认样式。

关于java - 创建自定义 TextButton 类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30445781/

相关文章:

java - LibGDX:为所有内容保留 1 个 SpriteBatch 而不丢弃它有什么危害?

java - 在 JApplet 中正确使用事件调度线程?

java - 在java中返回查询结果集的正确方法

java - j2objc gradle插件-在项目应用中找不到路径为 'test'的任务

java - 动画是抽象的,无法实例化

java - 将形状移动到手指触摸的位置

java - 关系表的 Spring JPA Repository 查询过滤器

android - 构建 Android NDK 项目的问题

java - 如何在 eclipse 摄入文本中制作文本框并将其用作变量

eclipse - 如何同时提取多个项目的更改