切换 JPanel 和 keyListener

发布于 2024-12-17 04:08:11 字数 921 浏览 1 评论 0原文

我正在开发一款游戏,你首先进入主屏幕,有多种选择,例如单人游戏、双人游戏、积分等。

我有一个大问题。如果我单击菜单中的按钮(不是 JButton),JPanel 会切换,但 keyListener 会丢失。 Keylistener 与游戏代码位于同一类中,它实现了 JPanel。我尝试了一切方法让 Keylistener 正常工作,但就是不行。

下面是这些东西的调用方式: Main class -->菜单-->游戏。我尝试将密钥侦听器添加到主类中,但它不起作用。

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

这是我当前的面板开关:

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

提前感谢您的帮助!

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.

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.

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.

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;
    }
}

Thanks for your help in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终止放荡 2024-12-24 04:08:11

您是否考虑过或尝试过使用按键绑定,而不是使用 KeyListener? KeyListener 要求被监听的组件具有焦点,并且焦点可能会由于多种原因而丢失,特别是在交换视图时(您是否为此使用了 CardLayout?)。另一方面,即使绑定的组件没有焦点,但当它仅包含在具有焦点的窗口中时,键绑定也可以设置为响应式。教程:使用 CardLayout

编辑
我发现您没有使用 CardLayout,我建议您使用它,因为它可以使您的视图交换更干净、更容易。

编辑2
我同意您不想在这里发布整个 2000 多行程序,因为没有人有时间阅读它,但请考虑将您的问题压缩成一个小类,该类可以由任何和所有的人编译和运行我们,并展示您的问题。换句话说,简短、独立、可编译、示例或 SSCCE

请记住,代码应该是可编译和可运行的,以便我们许多人能够完全理解它。

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

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.

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.

浪荡不羁 2024-12-24 04:08:11

重新聚焦时,卡片布局实际上很奇怪。

@op,尝试在添加新的 jpanel 后调用 requestFocusInWindow()

Cardlayout actually is screwy while refocusing.

@op, try calling requestFocusInWindow() after the new jpanel was added

So尛奶瓶 2024-12-24 04:08:11

尝试使用 myPanel.requsetFocusInWindow();
在使用 setVisible(true); 之前

Try using myPanel.requsetFocusInWindow();
before using setVisible(true);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文