检查在actionPerformed中是否已点击了键。

huangapple go评论78阅读模式
英文:

check if key has been clicked inside actionPerformed

问题

如何在actionPerformed方法内检查键盘输入或者检查键盘输入并将结果发送到actionPerformed方法?

我有3个文件:

主文件(main),只是调用了一个新的JFrame

public class Main {
    public static void main(String[] args) {
        new myJFrame();
    }
}

myJframe

public class myJFrame extends JFrame {
    myJPanel panel;

    myJFrame() {
        panel = new myJPanel();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(panel);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}

myJPanel

public class myJPanel extends JPanel implements ActionListener {
    final int PANEL_WIDTH = 500;
    final int PANEL_HEIGHT = 500;
    final int SNAKE_WIDTH = 25;
    final int SNAKE_HEIGHT = 25;
    JPanel snakeBody;
    Timer timer;
    int xVelocity = 1;
    int yVelocity = 1;
    int xPosition = 250;
    int yPosition = 250;

    myJPanel() {
        snakeBody = new JPanel();
        this.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
        timer = new Timer(10, this);
        timer.start();
    }

    public void paint(Graphics g) {
        super.paint(g); // 画背景。
        Graphics2D g2D = (Graphics2D) g;
        g2D.fillRect(xPosition, yPosition, SNAKE_WIDTH, SNAKE_HEIGHT);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }
}
英文:

how i can check for an keyboard input inside the actionPerformed method or chek for input from the keyboard and sent the result to the actionPerformed method?

i have 3 files:

main that just call new JFrame:

  public class Main {
      public static void main(String[] args) {
          new myJFrame();
      }
  }

myJframe:

public class myJFrame extends JFrame {
myJPanel panel;

    myJFrame(){
        panel = new myJPanel();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(panel);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}

and myJPanel:

    public class myJPanel extends JPanel implements ActionListener {
        final int PANEL_WIDTH = 500;
        final int PANEL_HEIGHT = 500;
        final int SNAKE_WIDTH = 25;
        final int SNAKE_HEIGHT = 25;
        JPanel snakeBody;
        Timer timer;
        int xVelocity = 1;
        int yVelocity = 1;
        int xPosition = 250;
        int yPosition = 250;
    
        myJPanel(){
            snakeBody = new JPanel();
            this.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
            timer = new Timer(10, this);
            timer.start();
    
        }
    
        public void paint(Graphics g){
            super.paint(g); // paint background.
            Graphics2D g2D = (Graphics2D) g;
            g2D.fillRect(xPosition, yPosition, SNAKE_WIDTH, SNAKE_HEIGHT);
    
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
}

答案1

得分: 1

使用键绑定来更改一个状态字段,也许是确定方向的字段,然后使用计时器的ActionListener来查询这个状态字段,并使用它来选择移动的方向。

我建议你使用key bindings(查看教程链接),以了解如何执行此操作。

顺便说一下:最好不要覆盖paint方法,而是覆盖paintComponent方法,因为这将为你提供双缓冲,并应该会导致更平滑的动画效果。

例如

英文:

Use key bindings to change a state field, perhaps one that determines direction, and then a timer's ActionListener to query this state field and use it to select a direction to move.

I recommend that you use key bindings (check the tutorial link), to show how to do this.

Side note: best to not override paint but rather paintComponent, since this will give you double-buffering out of the box and should result in smoother animation.

For example

huangapple
  • 本文由 发表于 2023年5月29日 20:38:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定