问题描述
我有一个显示缓冲图像的功能。我希望鼠标监听器和密钥监听器在JLabel中实现,它加载图像。由于JLabel无法获得焦点,我试图使用KeyBinding。但我注意到KeyBinding用于个人密钥。我想要的是每个按下的键的键码,而不管按下了什么键。有没有办法在这样的条件下获得KeyCode?另外我注意到KeyListener和MouseListener在构造函数中工作,但在其他方法中不工作。这是真的吗?
I have a function that display the buffered image. I want mouse listener and key listener to be implemented in JLabel which loads the image. Since JLabel cannot get focus i tried to use KeyBinding. But what i noticed that KeyBinding is used for individuals key. What i want is keycode of each pressed key irrespective of what key is pressed. is there way to get KeyCode in such conditions? Also i noticed that KeyListener and MouseListener works in Constructor but not in other methods. is it true?
public void imageloader(BufferedImage image) throws InterruptedException {
// frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dimension = new Dimension(image.getWidth(), image.getHeight());
setSize(200, 100);
setVisible(true);
label.removeAll(); //label is Jlabel
label.setIcon(new ImageIcon(image));
frame.setSize(dimension);
label.revalidate();
JScrollPane pane = new JScrollPane(label,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(pane);
frame.setSize(dimension);
// frame.setVisible(true);
}
更新:
我更改了方法,但仍无法获得KeyBoard Focus,但会触发MouseEvents。
I Changed the Method, still cannot get KeyBoard Focus, though MouseEvents are triggered .
public void imageloader(BufferedImage image) throws InterruptedException {
final String eol = System.getProperty("line.separator");
final JTextArea ta = new JTextArea(15, 60);
ta.setEditable(false);
Dimension dimension = new Dimension(Bimage.getWidth(), Bimage.getHeight());
gui.add(new JScrollPane(ta), BorderLayout.CENTER);
gui.add(l, BorderLayout.NORTH);
gui.setSize(dimension);
l.setFocusable(true);
//l.setOpaque(true);
l.removeAll();
l.setIcon(new ImageIcon(Bimage));
l.revalidate();
l.addFocusListener(new FocusListener() {
Color focused = Color.CYAN;
Color unfocused = Color.ORANGE;
public void focusLost(FocusEvent fe) {
System.out.println("Unfocused");
}
public void focusGained(FocusEvent fe) {
System.out.println("Focused");
}
});
l.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent me) {
System.out.println("Mouse Entered");
}
@Override
public void mouseExited(MouseEvent me) {
System.out.println("Mouse Exited");
}
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked");
}
});
l.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent ke) {
System.out.println("Key Typed");
}
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed");
}
});
frame.getContentPane().add(gui);
frame.setSize(dimension);
}
推荐答案
I don't know from where you knew JLabel can get Focus.
Documentation here clearly explains it cannot get Keyboard Focus.
让我们回到
myLabel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke((KeyEvent.DEL), 0, false), "DELETE");
myLabel.getActionMap().put("DELETE", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// any/some Action
}
});
这篇关于使用Keybinding获取keycode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!