我不确定为什么当我按 w 我的矩形没有相应调整。我是否正确设置了焦点,还是需要从单独的课程中请求它?我应该在我的drawingComponent
课程中还是在我的"核心"类?
package scratch;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.*;
public class drawingComponent extends JComponent implements KeyListener {
Rectangle hello = new Rectangle(300, 100, 50, 50);
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(255,25,0));
g2.setFont(new Font("monospace", Font.BOLD+Font.ITALIC, 30));
g2.drawString("nothing yet",300,320);
g2.fill(hello);
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W){
hello.setLocation(hello.x-50, hello.y);
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
我通过在drawingComponent
课程中添加以下内容解决了这个问题。
setFocusable(true);
requestFocus();
addKeyListener(this);
答案 0 :(得分:2)
你想使用Key Bindings,而不是KeyListener有几个原因,但有一个原因是你不必太担心使用Key Bindings。此外,您将来希望发布minimal example program我们可以测试,运行和修改的内容,如下所示:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class DrawingComponent extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 600;
private static final Color RECT_COLOR = new Color(255,25,0);
private Rectangle rect = new Rectangle(300, 100, 50, 50);
public DrawingComponent() {
setUpKeyBindings();
}
private void setUpKeyBindings() {
int condition = WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition);
ActionMap actionMap = getActionMap();
KeyStroke wStroke = KeyStroke.getKeyStroke(KeyEvent.VK_W, 0);
inputMap.put(wStroke, wStroke.toString());
actionMap.put(wStroke.toString(), new WAction());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(RECT_COLOR);
g2.fill(rect);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class WAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
rect.setLocation(rect.x-50, rect.y);
repaint();
}
}
private static void createAndShowGui() {
DrawingComponent mainPanel = new DrawingComponent();
JFrame frame = new JFrame("DrawingComponent");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
另外,在覆盖中调用super的paintComponent方法,否则你的JPanel不会删除旧图像。