按下按钮后的KeyListener [英] KeyListener after Button Is Pressed

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

问题描述

我正在创建一个小应用程序,如果按下大写锁定按钮,将添加一个托盘图标。我收到的错误似乎无法解决。

I'm creating a little application that will add a tray icon if the caps lock button is pressed. I'm getting a error that I can't seem to fix.

这是我的代码

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {       

    public void keyPressed(KeyEvent ke){

        //removed for StackOverflow

    }

 }   

我'得到非法开始表达错误的keyPressed听众
现在这可能是一个超级简单的修复,我只是不知道。

I'm getting the "Illegal start of expression" Error on the keyPressed Listener Now this is probably a super simple fix, I just don't know it.

推荐答案

KeyListener 只有在注册的组件具有焦点且具有焦点时才会被激活。如果UI有一些其他组件(如按钮或文本字段),那么它将无法工作。

KeyListener will only be activated if the component it is registered to is focusable AND has focus. If the UI has some other component (like a button or textfield) then it will not work.

我假设您想知道大写锁定状态更改的时间,无论您的计划中有什么焦点。为此,您可以使用 Toolkit 注册 AWTListener ,并监控应用程序处理的所有关键流量。

I'm assuming that you want to know whenever the caps lock state changes, regardless of what might have focus in your program. To this end you can register a AWTListener with the Toolkit and monitor ALL key traffic that is been processed by your application.

使用此侦听器,您可以监视 KeyEvent.VK_CAPS_LOCK 键事件并采取适当的操作。

With this listener, you could monitor of the KeyEvent.VK_CAPS_LOCK key event and take appropriate action.

如果您需要知道大写锁定键的确切状态,可以使用 Toolkit.getDefaultToolkit()。getLockingKeyState(KeyEvent.VK_CAPS_LOCK)确定它是打开还是关闭...

If you need to know the exact state of the caps lock key, you can use Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK) to determine if it is on or off...

import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestListener {

    public static void main(String[] args) {
        new TestListener();
    }

    public TestListener() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel capsStateLabel;

        public TestPane() {
            setLayout(new GridBagLayout());
            capsStateLabel = new JLabel();
            add(capsStateLabel);
            updateLabelState();
            Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

                @Override
                public void eventDispatched(AWTEvent event) {
                    if (event instanceof KeyEvent) {

                        KeyEvent ke = (KeyEvent) event;
                        switch (ke.getKeyCode()) {
                            case KeyEvent.VK_CAPS_LOCK:
                                switch (ke.getID()) {
                                    case KeyEvent.KEY_PRESSED:
                                    case KeyEvent.KEY_RELEASED:
                                        updateLabelState();
                                        break;
                                }
                                break;
                        }

                    }
                }

            }, KeyEvent.KEY_EVENT_MASK);
        }

        protected void updateLabelState() {
            boolean capsState = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
            if (capsState) {
                capsStateLabel.setText("Caps is ON");
            } else {
                capsStateLabel.setText("Caps is OFF");
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

这篇关于按下按钮后的KeyListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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