为什么我的java KeyListener不能为我的ubuntu工作?

时间:2016-01-22 00:45:31

标签: java swing keylistener

我在java中测试我的keyListener。

我在Ubuntu 14.04中的系统。我在main中设置了一个面板并初始化了关键监听器。我还将focusable设置为true并执行requestFocusInWindow。

但是当我运行程序时,println永远不会出现在控制台中。对此感到困惑。

package key;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Key extends JPanel{
public void action(){
    KeyListener k = new KeyListener(){
        @Override
        public void keyPressed(KeyEvent k){
            System.out.println("key is pressed!");
        }
        @Override
        public void keyReleased(KeyEvent e){
            // TODO Auto-generated method stub
        }
        @Override
        public void keyTyped(KeyEvent e){
            System.out.println("key is typed!");
        }
    };
    this.addKeyListener(k);
    this.setFocusable(true);
    this.requestFocusInWindow();
}
public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setSize(400,300);
    JPanel panel = new JPanel();
    panel.setBackground(Color.BLUE);
    frame.add(panel);
    frame.setVisible(true);

    Key k = new Key();
    k.action();
}
}

1 个答案:

答案 0 :(得分:2)

  

有什么建议吗?

例如......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Key extends JPanel {

    public void action() {
        JLabel label = new JLabel("Waiting");
        InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = getActionMap();

        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "press.a");
        actionMap.put("press.a", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("Pressed A");
            }
        });
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "release.a");
        actionMap.put("release.a", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("Waiting");
            }
        });

        add(label);
    }

    public static void main(String[] args) {
        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);
                JPanel panel = new JPanel();
                panel.setBackground(Color.BLUE);
                frame.add(panel);
                Key k = new Key();
                k.action();
                frame.add(k, BorderLayout.SOUTH);
                frame.setSize(400, 300);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}