然后再看看Button吧,实例化需要一个ButtonStyle,定义了按钮三种状态对应的图片样式,按下和松开时的X,Y偏移还有Button中文字绘制所需的BitmapFont和Color。

       按钮的三种状态的图片我就省了,只用一张图片。

Android游戏引擎libgdx使用教程5:常用UI类与舞台

       修改代码如下:

Java代码
  1. package com.cnblogs.htynkn.listener;    

  2. import com.badlogic.gdx.ApplicationListener;    

  3. import com.badlogic.gdx.Gdx;    

  4. import com.badlogic.gdx.graphics.Color;    

  5. import com.badlogic.gdx.graphics.GL10;    

  6. import com.badlogic.gdx.graphics.Texture;    

  7. import com.badlogic.gdx.graphics.g2d.BitmapFont;    

  8. import com.badlogic.gdx.graphics.g2d.NinePatch;    

  9. import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;    

  10. import com.badlogic.gdx.scenes.scene2d.Stage;    

  11. import com.badlogic.gdx.scenes.scene2d.actors.Label;    

  12. import com.badlogic.gdx.scenes.scene2d.ui.Button;    

  13. import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;    

  14. public class FirstGame implements ApplicationListener {    

  15. private Stage stage;    

  16. Label label;    

  17. Texture texture;    

  18. Button button;    

  19. @Override    

  20. public void create() {    

  21. stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),    

  22. true);    

  23. texture = new Texture(Gdx.files.internal("06.png"));    

  24. NinePatch n1 = new NinePatch(texture, 7799);    

  25. BitmapFont bitmapFont = new BitmapFont(Gdx.files.internal("cf.fnt"),    

  26. Gdx.files.internal("cf.png"), false);    

  27. label = new Label("fpsLabel", bitmapFont, "label1");    

  28. label.x = 5; &n