焦点Java小程序 [英] focus for java applets

查看:137
本文介绍了焦点Java小程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所做的都是基于一个的JPanel Java游戏。当我把它添加到一个JFrame它完美的作品,当我把它添加到JApplet的并测试它使用Eclipse的内置小程序测试它完美。然而,当我试图通过一个HTML网站运行小程序不起作用。它加载,但它没有考虑键盘输入。我有它设置,其中pressing的开始游戏,但即使当我点击游戏和preSS S,什么都不会发生。

我必须设置键盘焦点,因为我认为这是自动完成的。


解决方案

  

,但它没有考虑键盘输入。


的KeyEvent只传递给具有焦点的组件。我猜你的面板没有焦点,所以确保你的面板可聚焦,然后使用requestFocusInWindow()方法,一旦GUI是可见的,以确保面板具有焦点。

然而,更好的解决问题的方法不是靠的KeyListener而是使用键绑定。秋千被设计为使用键绑定。

了解更多关于键绑定

I made a java game that is all based on JPanel. When I add it to a JFrame it works perfectly, and when I add it to a JApplet and test it with the built in applet tester of eclipse it works perfectly. However, when I try to run the applet through an html site it doesn't work. It loads, but it doesn't take keyboard input. I have it set up where "pressing s" starts the game, but even when I click on the game and press s, nothing happens.

Do I have to set the keyboard focus, because I thought that was done automatically.

解决方案

but it doesn't take keyboard input.

KeyEvents are only passed to the component that has focus. I would guess your panel doesn't have focus, so make sure you make the panel focusable and then use the requestFocusInWindow() method once the GUI is visible to make sure the panel has focus.

However, the better solution to the problem is not to rely on the KeyListener but instead to use Key Bindings. Swing was designed to use Key Bindings.

Read more about Key Bindings.

这篇关于焦点Java小程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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