我不确定为什么当我按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);

最佳答案

出于多种原因,您想使用Key Bindings而不是KeyListener,但是其中一个原因是您不必太担心键绑定的重点。另外,您将来希望发布一个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();
         }
      });
   }
}


另外,在您的覆盖中调用上级的paintComponent方法,否则您的JPanel不会删除旧图像。

关于java - 请求重点关注监听器,不确定我是否有重点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26831196/

10-11 23:02