Dialog在游戏中也很常用,尤其在设置、退出、商店、暂停等画面。Dialog的使用也可以通过skin实现,也可以自定义。

下面是一个简单的实例:

 package com.fxb.newtest;

 import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable; public class Lib007_Dialog implements ApplicationListener{ Stage stage;
Dialog dialog;
Drawable draw1;
Drawable draw2;
Pixmap pixmap;
Texture texture;
Skin skin;
BitmapFont font;
Dialog dialog2; @Override
public void create() {
// TODO Auto-generated method stub stage = new Stage();
skin = new Skin();
pixmap = new Pixmap( , , Format.RGBA8888 );
pixmap.setColor( Color.GRAY );
pixmap.fill();
//draw1 = new TextureRegionDrawable( );
//texture = new Texture( pixmap );
skin.add( "gray", new Texture(pixmap) ); pixmap.setColor( Color.LIGHT_GRAY );
pixmap.fill();
skin.add( "light_gray", new Texture(pixmap) ); font = new BitmapFont();
WindowStyle windowStyle = new WindowStyle( font, Color.GREEN, skin.getDrawable( "light_gray" ) );
dialog = new Dialog( "DialogTest", windowStyle );
dialog.setTitleAlignment( Align.center | Align.top );
dialog.setBounds( , , , ); LabelStyle labelStyle = new LabelStyle( font, Color.RED );
ButtonStyle buttonStyle = new ButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null );
TextButtonStyle textbuttonStyle = new TextButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null, font );
skin.add( "default", buttonStyle );
skin.add( "default", textbuttonStyle );
skin.add( "default", windowStyle );
skin.add( "default", labelStyle ); Button button1 = new Button( skin );
button1.setBounds( , , , );
TextButton textbutton1 = new TextButton( "Click", skin );
textbutton1.setBounds( -, , , );
dialog.addActor( button1 );
dialog.addActor( textbutton1 ); stage.addActor( dialog );
//dialog2.row().fill().expand(); dialog2 = new Dialog( "Dialog2", skin, "default" ){
@Override
protected void result(Object object) {
// TODO Auto-generated method stub
//System.out.println( "Chosen" + object );
if( object.toString().equals( "true" ) ){
System.out.println( "Yes" );
Gdx.app.exit();
}else{
System.out.println( "No" );
}
}
};
dialog2.text("\nDo you want to exit?").button("Yes", true).button("No",false).key(Keys.ENTER, true).key(Keys.ESCAPE,false);
dialog2.setTitleAlignment( Align.top );
dialog2.pack();
//dialog2.show( stage ); //TextButton textbutton2 = new TextButton( "Click", skin );
textbutton1.addListener( new ChangeListener()
{
@Override
public void changed(ChangeEvent event, Actor actor) {
// TODO Auto-generated method stub
dialog2.show( stage );
}
}); //stage.addActor( textbutton2 );
Gdx.input.setInputProcessor( stage );
} @Override
public void resize(int width, int height) {
// TODO Auto-generated method stub } @Override
public void render() {
// TODO Auto-generated method stub
Gdx.gl.glClearColor( , , , );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); stage.act();
stage.draw(); } @Override
public void pause() {
// TODO Auto-generated method stub
} @Override
public void resume() {
// TODO Auto-generated method stub
} @Override
public void dispose() {
// TODO Auto-generated method stub
skin.dispose();
stage.dispose();
texture.dispose();
} }

运行效果:

最新文章

  1. docker 学习过程
  2. python ide ---wing 注册机
  3. 个人建了一个APPCAN移动前端开发交流QQ群258213194
  4. EntityFramework 中支持 BulkInsert 扩展
  5. php读取excel文件 更新修改excel
  6. BZOJ 2301 Problem b
  7. DIV+CSS 基础
  8. word2003公式编辑器公式显示不完整问题
  9. class不想被复制的两个做法
  10. 【HDU1712】ACboy needs your help(分组背包)
  11. UI 公钥加密
  12. Javascript多线程引擎(六)
  13. MySQL最常用字符串函数
  14. oracle数据库与实例
  15. RabbitMQ 和 Kafka
  16. Linux 系统负载查询及分析说明
  17. Swift 通过字符串创建控制器
  18. python3用BeautifulSoup用re.compile来匹配需要抓取的href地址
  19. tls/ssl工作原理及相关技术
  20. ETCD分布式存储部署

热门文章

  1. Python+Selenium笔记(五):生成测试报告
  2. win10 安装microsoft.net framework3.5
  3. poj_3253 Fence Repair
  4. fatal: cannot create directoryxxxx': Invalid argument
  5. Linux每日小技巧---统计服务器IP连接数
  6. C++课堂作业_02_PAT1025.反转链表
  7. 028、HTML 标签3表单标签插入组件
  8. phpstorm 的.idea 目录加入.gitignore无效的解决方法
  9. jQuery1.9+ 废弃的函数和方法 升级Jquery版本遇到的问题
  10. Android-硬件加速