切换JPanel和keyListener [英] Switching JPanels and keyListeners

查看:135
本文介绍了切换JPanel和keyListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个游戏,您首先进入主屏幕,这里有多个选择,例如,单人游戏,双人游戏,积分等.

I am developing a game, where you first get to the main screen, there are multiple selections to go, for example, Singleplayer, Twoplayer, Credits, etc.

我有一个大问题.如果单击菜单中的一个按钮(不是JButton),而不是JPanel开关,但是keyListener将丢失. Keylistener与实现JPanel的游戏代码在同一类中.我尽了一切努力使Keylistener正常工作,但事实并非如此.

I have one big problem. If I click a button in the menu, (not JButton) the JPanels switch, but the keyListener is lost. The Keylistener is in the same class as the game code, which implements JPanel. I tried everything to get the Keylistener to work, but it just won't.

这里是事物的称呼方式:主类->菜单->游戏.我尝试将keylistener添加到主类,但无法正常工作.

Here is how the things are called: Main class --> Menu --> Game. I tried adding the keylistener to the main class, but it's not working.

因此,JPanel切换正常,但Keylistener消失了.我以前使用新的JFrame开发游戏,因此,当我单击菜单时,会创建一个新的框架.我没有在这里插入代码,因为它太长了(超过2000行),并且KeyListener正在工作,但仅当它在新的JFrame中时才起作用.我通过单击一个按钮在Menu类中设置了int模式.

So, JPanel switching is ok, but the Keylisteners are gone. I was developing the game before with new JFrames, so when I clicked a menu, a new frame was created. I didn't insert a code here, because it's too long (2000+ lines), and the KeyListener is working, but only when it is in a new JFrame. I set the mode int in the Menu class, by clicking a button.

这是我目前的面板开关:

This is currently my panel switch:

public void setJPanel() {
     switch (mode) {
        case 1:
            getContentPane().add(s);
            validate();
            break;
        case 2:
            getContentPane().removeAll();
            getContentPane().add(sp);
            validate();
            break;
    }
}

感谢您的提前帮助!

推荐答案

您是否考虑过或尝试过使用键绑定而不是使用KeyListener? KeyListener要求被侦听的组件具有焦点,并且焦点可能由于多种原因而丢失,尤其是在交换视图时(您是否为此使用CardLayout?).另一方面,即使绑定的组件不具有焦点,而仅在具有焦点的窗口中保留时,也可以将键绑定"设置为响应式的.教程:使用CardLayout

Rather than use a KeyListener, have you given thought to or tried using Key Bindings? KeyListeners require that the component being listened to has focus, and focus may be lost for many reasons, especially when swapping views (are you using a CardLayout for this?). Key Bindings on the other hand can be set to be responsive even if the bound component doesn't have focus but when it is only held within a window that has focus. Tutorial: Using a CardLayout

修改
我们发现您没有使用CardLayout,因此建议您使用CardLayout,因为它可以使视图交换更简洁,更轻松.

Edit
I see that you're not using a CardLayout, and I suggest that you use this as it can make your view swapping cleaner and easier.

编辑2
我同意您不想在这里发布整个2000+行程序,因为没有人有时间阅读它,但可以考虑将您的问题/问题浓缩为一个小类,该类可以由任何其他人编译和运行我们,并演示您的问题.换句话说,是简短,自包含,可编译,示例或SSCCE .

Edit 2
I agree that you don't want to post your entire 2000+ line program here as no one will have the time to read it, but consider condensing your question/problem into a single small class that is compilable and runnable by any and all of us, and demonstrates your problem. In other words, a Short, Self Contained, Compilable, Example or SSCCE .

请记住,对于我们许多人来说,这些代码应该是可编译且可运行的,以便能够完全理解它.

Remember, the code should be compilable and runnable for many of us to be able to understand it fully.

这篇关于切换JPanel和keyListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆