为什么键侦听器停止工作? [英] Why does the keylistener stop working?

查看:74
本文介绍了为什么键侦听器停止工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java程序中,每当我从JTextField中选择一些文本时,keyListener都会停止检测按键.我注意到按下JButton时也会发生同样的事情.使用对象后是否需要从对象中删除keyListener?如果是这样,我该怎么做?

In my Java program, whenever I select some text from a JTextField, the keyListener stops detecting key presses. I noticed the same thing happens when a JButton is pressed. Do I need to remove the keyListener from the objects after using them? If so, how to I do this?

这是我遇到问题的程序的副本:

Here is a copy of the program I'm having problems with:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ColourDropper extends JPanel implements KeyListener, ActionListener {

    private Color background = Color.WHITE;
    private int mouseX, mouseY;
    private Timer t;
    private JTextField rgb, hsb, hex, alp;
    private JLabel tRgb, tHsb, tHex, tHold, tAlp;
    private String hexString;
    private boolean hold = false;

    public ColourDropper() {
        this.setFocusable(true);
        t = new Timer(100, this);
        t.start();
        rgb = new JTextField(7);
        hsb = new JTextField(9);
        hex = new JTextField(6);
        alp = new JTextField(3);
        tRgb = new JLabel("RGB");
        tHsb = new JLabel("HSB");
        tHex = new JLabel("Hex");
        tAlp = new JLabel("Alpha");

        rgb.setEditable(false);
        hsb.setEditable(false);
        hex.setEditable(false);
        alp.setEditable(false);

        add(tRgb);
        add(rgb);
        add(tHex);
        add(hex);
        add(tHsb);
        add(hsb);
        add(tAlp);
        add(alp);
        addKeyListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if(!hold) {
            mouseX = MouseInfo.getPointerInfo().getLocation().x;
            mouseY = MouseInfo.getPointerInfo().getLocation().y;

            try {   
                Robot robot = new Robot();
                background = robot.getPixelColor(mouseX, mouseY);
                hexString = "#" + Integer.toHexString(background.getRGB()).toUpperCase().substring(2);
            } catch(AWTException a) {
                System.out.println(a.getMessage());
            } catch(Exception x) {
                System.out.println(x.getMessage());
            }

            try {
                rgb.setText(background.getRed() + " " + background.getGreen() + " " + background.getBlue());
                float[] cHsb = Color.RGBtoHSB(background.getRed(), background.getGreen(), background.getBlue(), null);
                int hue = (int)(cHsb[0] * 360);
                int sat = (int)(cHsb[1] * 100);
                int bri = (int)(cHsb[2] * 100);
                hsb.setText(hue + "� " + sat + "% " + bri + "%");
                hex.setText(hexString);
                alp.setText("" + background.getAlpha());
            } catch(NullPointerException n) {
                System.out.println(n.getMessage());
            }

            repaint();
        }
    }

    public void keyPressed(KeyEvent e) {        
        if(e.getKeyCode() == KeyEvent.VK_SPACE) hold = !hold;
        if(hold) {
            rgb.setForeground(Color.RED);
            hex.setForeground(Color.RED);
            hsb.setForeground(Color.RED);
            alp.setForeground(Color.RED);
        } else {
            rgb.setForeground(Color.BLACK);
            hex.setForeground(Color.BLACK);
            hsb.setForeground(Color.BLACK);
            alp.setForeground(Color.BLACK);
        }
    }

    public void paintComponent(Graphics g) {
        g.setColor(new Color(238, 238, 238));
        g.fillRect(0, 0, 246, 120);
        g.setColor(background);
        g.fillRect(5, 57, 230, 30);
    }

    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}

    public static void main(String[] args) {
        JFrame frame = new JFrame("Colour Dropper");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(246, 120));
        frame.pack();
        frame.setVisible(true);
        ColourDropper frameContent = new ColourDropper();
        frame.add(frameContent);
        frame.setResizable(false);
        frame.setLocation(100, 100);
        frame.setAlwaysOnTop(true);
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage("dropper.png"));
    }
}

推荐答案

为使KeyListener正常工作,正在侦听的组件必须具有焦点.一旦将焦点转移到其他地方,KeyListener就会失败.通常,最好改用按键绑定.

For a KeyListener to work, the component that is being listened to must have the focus. As soon as the focus is directed elsewhere, the KeyListener fails. Often you're better off using key bindings instead.

这篇关于为什么键侦听器停止工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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