执行键盘操作时遇到问题

发布于 2024-12-02 23:02:28 字数 1337 浏览 4 评论 0原文

我根本不明白如何实现键盘操作。

鼠标点击、按钮、文本字段、文本区域我都很好,键盘对我来说就像中国墙。

我有这样的东西,我想实现键盘在按“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 技术交流群。

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

发布评论

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

评论(4

情绪失控 2024-12-09 23:02:28

我首先查看按键绑定。这比 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.

围归者 2024-12-09 23:02:28

我想实现键盘在按“C”时关闭:

然后您应该创建一个自定义操作并使用带有关闭菜单项和加速器的 JMenu。

关闭应用程序ExitAction > 将为您做这件事。

and I'd like to implement the Keyboard to close when I press "C":

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.

原谅过去的我 2024-12-09 23:02:28

您尚未将 KeyboardListener 连接到组件。您也没有使用类中定义的 anEventListener 字段 - 它被隐藏在 initGUI 内部。

You haven't attached your KeyboardListener to a component. You also aren't using the anEventListener field defined in your class -- it's being shadowed inside initGUI.

你在我安 2024-12-09 23:02:28

只需添加一行

myFrame.addKeyListener(anEventListener);

来在框架中注册您的侦听器,它就会正常工作。

注意:这只会处理与您的框架关联的关键事件。如果您周围有其他组件,您可能希望以不同的方式处理它(另请参阅如何使用键绑定)。

在你的情况下,你可以很容易地构建一个带有键绑定的版本:

JComponent rootPane = myFrame.getRootPane();
rootPane.getInputMap().put(KeyStroke.getKeyStroke("C"), "closeThisOne");
rootPane.getActionMap().put("closeThisOne", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
});

Just add the line

myFrame.addKeyListener(anEventListener);

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:

JComponent rootPane = myFrame.getRootPane();
rootPane.getInputMap().put(KeyStroke.getKeyStroke("C"), "closeThisOne");
rootPane.getActionMap().put("closeThisOne", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文