我在 JPanel 类中创建了一个 KeyAdapter 类,并尝试使用 System.out.println(String par1); 调试单击
但是似乎不起作用...
这是类(class):
package net.ryanshah;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Menu extends JPanel
{
private GameFrame gf;
private Image bg = new ImageIcon("res/bg.png").getImage();
public Menu(final GameFrame parent) {
this.gf = parent;
int width = parent.getWidth();
int height = parent.getHeight();
setFocusable(true);
addKeyListener(new MenuOperator());
setBackground(Color.black);
setLayout(null);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
g2d.drawImage(bg, 0, 0, null);
}
public class MenuOperator extends KeyAdapter {
@Override
public void keyPressed(KeyEvent event) {
int keyCode = event.getKeyCode();
if(keyCode == KeyEvent.VK_SPACE) {
System.out.println("LOL");
}
}
}
}
有谁知道问题出在哪里吗?我已在框架中设置了关键处理程序并将框架设置为可聚焦..
提前致谢
~瑞安S。
最佳答案
代码中未考虑的事情(恕我直言):
- 您在代码中访问图像的方式。考虑ImageIO ,而不是使用
ImageIcon
的构造函数,因为前者会通知您,以防出现问题。欲了解更多信息,请参阅Loading Image Icon Exception ,以获得更多见解。 KeyListeners
适用于AWT
,而Swing
使用 KeyBindings .- 之前,在任何
JComponent
上设置背景时,首先将所述JComponent
的opaque
属性设置为true
>。因为,opaque
属性通常依赖于所使用的LookAndFeel
。 - 尽可能避免使用
setLayout(null)
。请浏览Absolute Positioning的第一段欲了解更多信息:-) - 评论中已经提到了
ImageObserver
的使用。
此博客Motion Using the Keyboard作者:@camickr,将再次增加您的知识,例如为什么 KeyListeners
不被认为是更好的选择以及为什么 KeyBindings
更适合此类需求:-)
请看一下这个例子:
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TestMenu extends JPanel {
private Image bg;
public TestMenu() {
try {
bg = ImageIO.read(
new URL("http://i.imgur.com/Aoluk8n.gif"));
} catch (Exception e) {
e.printStackTrace();
}
getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "SPACE key");
getActionMap().put("SPACE key", keyAction);
setOpaque(true);
setBackground(Color.black);
}
private Action keyAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
System.out.println("LOL");
}
};
/*
* Make this a customary habbit to override
* this method for the JPanel/JComponent,
* when ever you extend any of the above classes
*/
@Override
public Dimension getPreferredSize() {
return (new Dimension(200, 200));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
/*
* Here the last part of the drawImage is the
* ImageObserver, here it is a good practise
* to keep the instance of the JPanel on which
* we drawing the image to be notified, till the
* end, till the image loading is not complete
*/
g2d.drawImage(bg, 0, 0, this);
}
private void displayGUI() {
JFrame frame = new JFrame("Painting Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setContentPane(this);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new TestMenu().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
关于java - key 适配器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18536954/