libgdx退出对话框
package com.fxb.newtest; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.InputAdapter; import com.badlogic.gdx.InputMultiplexer; 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.Texture; import com.badlogic.gdx.graphics.Pixmap.Format; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Dialog; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle; import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; 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; public class Lib005_CancelKey extends InputAdapter implements ApplicationListener{ Stage stage; Skin skin; Dialog dialog1, dialog2; @Override public void create() { // TODO Auto-generated method stub CreateSkin(); stage = new Stage(); InputMultiplexer multiplexer = new InputMultiplexer(); multiplexer.addProcessor( stage ); multiplexer.addProcessor( this ); Gdx.input.setInputProcessor( multiplexer ); Gdx.input.setCatchBackKey( true ); Gdx.input.setCatchMenuKey( true ); TextButton textbutton1 = new TextButton( "Menu", skin ); TextButton textbutton2 = new TextButton( "Exit", skin ); textbutton1.addListener( new ChangeListener(){ @Override public void changed(ChangeEvent event, Actor actor) { // TODO Auto-generated method stub dialog1.show(stage); } }); textbutton2.addListener( new ChangeListener(){ @Override public void changed(ChangeEvent event, Actor actor) { // TODO Auto-generated method stub dialog2.show(stage); } }); textbutton1.setPosition( 100, 100 ); textbutton2.setPosition( 200, 100 ); stage.addActor( textbutton1 ); stage.addActor( textbutton2 ); } private void CreateSkin(){ skin = new Skin(); Pixmap pixmap = new Pixmap( 1, 1, 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) ); BitmapFont font = new BitmapFont(); WindowStyle windowStyle = new WindowStyle( font, Color.GREEN, skin.getDrawable( "light_gray" ) ); 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 ); dialog1 = new Dialog( "Menu", 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" ); } }}; dialog1.text("\nMenu").button("Yes", true).button("No",false).key(Keys.ENTER, true).key(Keys.ESCAPE,false); dialog1.setTitleAlignment( Align.top ); dialog2 = new Dialog( "Exit", 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 ); } @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( 1, 1, 1, 1 ); 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 } @Override public boolean keyDown(int keycode) { // TODO Auto-generated method stub if( keycode == Input.Keys.BACK ){ Gdx.app.log( "back", "" ); dialog2.show(stage); } if( keycode == Input.Keys.MENU ){ Gdx.app.log( "menu", "" ); dialog1.show(stage); } return false; } }
libgdx开发中返回键(cancel)和菜单键(menu)的使用。
开始时需要设置Gdx.input.enableCancelKey或者Gdx.input.enableMenuKey,然后捕获到相应事件时添加事件响应即可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!