Java 如何在 Libgdx 中创建一个按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21488311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a button in Libgdx?
提问by julian
I want to create a button that changes when the user hovers it, or clicking it. I created the following variable
我想创建一个按钮,当用户悬停或单击它时会发生变化。我创建了以下变量
Button buttonPlay = new Button();
I don't know what to do now, how to load the images? How to write text into the button? How to implement the events / effects (hover, click)?
我现在不知道该怎么办,如何加载图像?如何在按钮中写入文本?如何实现事件/效果(悬停、点击)?
It would be very helpful if someone could write some example code for a button.
如果有人可以为按钮编写一些示例代码,那将非常有帮助。
采纳答案by danielz
A button is simply an actor in libgdx. To render an actor you use a stage that contains all the actors of the screen, renders them and updates them. I assume you want a button with text, so you should use the class TextButton and add it to a stage. A TextButton takes a string to render and a ButtonStyle, in this case a TextButtonStyle, which is basically a class that contains all the information about the button (font, drawable to render while not pressed, drawable to render while pressed etc).
按钮只是 libgdx 中的一个角色。要渲染演员,您可以使用包含屏幕所有演员的舞台,渲染他们并更新他们。我假设您想要一个带有文本的按钮,因此您应该使用 TextButton 类并将其添加到舞台。TextButton 接受一个要渲染的字符串和一个 ButtonStyle,在本例中为 TextButtonStyle,它基本上是一个包含有关按钮的所有信息的类(字体、可绘制以在未按下时呈现、可绘制以在按下时呈现等)。
public class ButtonExample extends Game{
Stage stage;
TextButton button;
TextButtonStyle textButtonStyle;
BitmapFont font;
Skin skin;
TextureAtlas buttonAtlas;
@Override
public void create() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
font = new BitmapFont();
skin = new Skin();
buttonAtlas = new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
skin.addRegions(buttonAtlas);
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.up = skin.getDrawable("up-button");
textButtonStyle.down = skin.getDrawable("down-button");
textButtonStyle.checked = skin.getDrawable("checked-button");
button = new TextButton("Button1", textButtonStyle);
stage.addActor(button);
}
@Override
public void render() {
super.render();
stage.draw();
}
}
So whats happening here? I am creating a stage, a font and a textureatlas with all the textures for the buttons in "buttons.pack". Then I initialize an empty TextButtonStyle and and i add the font and the textures for the up, down and checked states. font, up, down and checked are all static variables of type Drawable so you can really pass it any kind of Drawable (texture, 9-patch etc). Then simply add the button to the Stage.
那么这里发生了什么?我正在创建一个舞台、一个字体和一个纹理图集,其中包含“buttons.pack”中按钮的所有纹理。然后我初始化一个空的 TextButtonStyle,并为向上、向下和选中状态添加字体和纹理。font、up、down 和checked 都是Drawable 类型的静态变量,因此您可以真正将任何类型的Drawable(纹理、9-patch 等)传递给它。然后只需将按钮添加到舞台。
Now in order to do something when the button is actually clicked, you have to add a listener to the button, a ChangeListener.
现在为了在按钮被实际点击时做一些事情,你必须向按钮添加一个监听器,一个 ChangeListener。
button.addListener(new ChangeListener() {
@Override
public void changed (ChangeEvent event, Actor actor) {
System.out.println("Button Pressed");
}
});
Of course instead of adding the button directly to the Stage you should add it to a Table and add the Table to the Stage but I didn't want to make this post too confusing. Hereis a good tutorial on tables in libgdx.
当然,不是将按钮直接添加到舞台,您应该将它添加到表格并将表格添加到舞台,但我不想让这篇文章太混乱。这是关于 libgdx 中表格的一个很好的教程。
回答by gokul sreenivasan
buttons.pack is the file generated from the libgdx texture packer, texture packer is the tool which can used to generate the texture atlas , That is multiple images you can loaded to the GUI using a single file . it also help to save some memory please refer this link`https://code.google.com/p/libgdx-texturepacker-gui/downloads/list, https://github.com/libgdx/libgdx/wiki/Texture-packer
button.pack 是 libgdx 纹理打包器生成的文件,texture packer 是可以用来生成纹理图集的工具,即可以使用单个文件将多个图像加载到 GUI 中。它还有助于节省一些内存,请参阅此链接` https://code.google.com/p/libgdx-texturepacker-gui/downloads/list, https://github.com/libgdx/libgdx/wiki/Texture-包装工