gpt4 book ai didi

java - 为什么 keylistener 停止工作?

转载 作者:行者123 更新时间:2023-12-02 07:45:12 25 4
gpt4 key购买 nike

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

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

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 就会失败。通常,最好改用键绑定(bind)。

关于java - 为什么 keylistener 停止工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7543628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com