执行键盘操作时遇到问题
我根本不明白如何实现键盘操作。
鼠标点击、按钮、文本字段、文本区域我都很好,键盘对我来说就像中国墙。
我有这样的东西,我想实现键盘在按“C”时关闭:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestGUI
{
private KeyboardListener anEventListener;
public TestGUI()
{
initGUI();
}
private void initGUI()
{
//Prepare Frame
JFrame myFrame = new JFrame();
myFrame.setTitle("Test");
myFrame.setSize(550, 500);
myFrame.setLocation(600, 100);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setLayout(null);
KeyboardListener anEventListener = new KeyboardListener();
//Show Frame
myFrame.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TestGUI();
}
});
}
class KeyboardListener implements KeyListener
{
public void keyPressed (KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.VK_C)
{
System.exit(0);
}
}
public void keyReleased(KeyEvent event)
{
}
public void keyTyped (KeyEvent event)
{
}
}
}
I don't get how do implement Keyboard actions at all.
Mouse clicks, Buttons, Textfield, Textarea I get just fine, Keyboard is like the Chinese wall to me.
I have something like this, and I'd like to implement the Keyboard to close when I press "C":
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestGUI
{
private KeyboardListener anEventListener;
public TestGUI()
{
initGUI();
}
private void initGUI()
{
//Prepare Frame
JFrame myFrame = new JFrame();
myFrame.setTitle("Test");
myFrame.setSize(550, 500);
myFrame.setLocation(600, 100);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setLayout(null);
KeyboardListener anEventListener = new KeyboardListener();
//Show Frame
myFrame.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TestGUI();
}
});
}
class KeyboardListener implements KeyListener
{
public void keyPressed (KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.VK_C)
{
System.exit(0);
}
}
public void keyReleased(KeyEvent event)
{
}
public void keyTyped (KeyEvent event)
{
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我首先查看按键绑定。这比 KeyListeners 更可靠,因为它没有太多焦点问题。另外,
KeyListeners
是针对此类问题的旧 AWT 解决方案。I would start by checking out Key Bindings. This is more reliable than
KeyListeners
as it doesn't have many focus issues. Also,KeyListeners
is an old AWT solution for problems like this.然后您应该创建一个自定义操作并使用带有关闭菜单项和加速器的 JMenu。
关闭应用程序ExitAction > 将为您做这件事。
Then you should create a custom Action and use a JMenu with a close menu item and an accelerator.
The
ExitAction
from Closing an Application will do this for you.您尚未将
KeyboardListener
连接到组件。您也没有使用类中定义的anEventListener
字段 - 它被隐藏在initGUI
内部。You haven't attached your
KeyboardListener
to a component. You also aren't using theanEventListener
field defined in your class -- it's being shadowed insideinitGUI
.只需添加一行
来在框架中注册您的侦听器,它就会正常工作。
注意:这只会处理与您的框架关联的关键事件。如果您周围有其他组件,您可能希望以不同的方式处理它(另请参阅如何使用键绑定)。
在你的情况下,你可以很容易地构建一个带有键绑定的版本:
Just add the line
to register your listener within your frame and it will work fine.
Note: This will only handle the key events associated with your frame. If you have other components around you might want to handle it differently (see also how to use key bindings).
In your case you can build a version with key bindings quite easily: