使用按键绑定获取键码
我有一个显示缓冲图像的功能。我希望在加载图像的 JLabel 中实现鼠标侦听器和按键侦听器。由于 JLabel 无法获得焦点,我尝试使用 KeyBinding。但我注意到 KeyBinding 用于个人密钥。我想要的是每个按下的键的键码,无论按下哪个键。在这种情况下有办法获取 KeyCode 吗?我还注意到 KeyListener 和 MouseListener 在构造函数中工作,但在其他方法中不起作用。这是真的吗?
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);
}
更新:
我更改了方法,尽管触发了 MouseEvents,但仍然无法获得键盘焦点。
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 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);
}
UPDATE:
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们回到 KeyBindings
let's back to the KeyBindings
当然可以。
现在更新
我已经更仔细地阅读了问题,这里是检测标签中焦点、鼠标和键键入事件的示例。
输出示例
Sure it can.
Update
Now I've read the question more carefully, here is an example of detecting focus, mouse and key typed events in a label.
Example output