ActionPerformed不在自定义组件中工作

时间:2016-05-23 19:48:13

标签: java swing actionlistener

我正在尝试编写一个代码,创建一个我称之为枪的东西,我希望能够向右或向左移动它。我实施了ActionListeneractionPerformed方法,但keyPressed无效。但是,public class Gun extends JPanel implements KeyListener, ActionListener { /** * */ private static final long serialVersionUID = 1L; private static final int GUN_WIDTH = 100; private static final int GUN_HEIGHT = 30; private static final int GUN_UPPER_HEIGHT = 20; private static final int GUN_UPPER_WIDTH = GUN_WIDTH / 4; int x, y; int velX; public Gun() { x = 250 - GUN_WIDTH / 2; y = 500 - GUN_HEIGHT; velX = 0; addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; int upperX = x + GUN_WIDTH / 2 - GUN_UPPER_WIDTH / 2; int upperY = y - GUN_UPPER_HEIGHT; g2d.setColor(Color.BLACK); g2d.fillRect(x, y, GUN_WIDTH, GUN_HEIGHT); g2d.fillRect(upperX, upperY, GUN_UPPER_WIDTH, GUN_UPPER_HEIGHT); } public void left() { velX = -1; } public void right() { velX = 1; } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) { left(); System.out.println("LEFT"); } if (key == KeyEvent.VK_RIGHT) { right(); System.out.println("RIGHT"); } } @Override public void keyReleased(KeyEvent arg0) { } @Override public void keyTyped(KeyEvent arg0) { } @Override public void actionPerformed(ActionEvent e) { x += velX; repaint(); } public static void main(String[] args) { Gun gun = new Gun(); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Game"); frame.setLayout(new FlowLayout()); gun.setPreferredSize(new Dimension(500, 500)); frame.add(gun); frame.setVisible(true); frame.pack(); } } 方法确实有效。我错过了什么想法?

return function (input) {
  return (!!input) ? input.replace(/([^\W_]+[^\s-]*) */g, function (txt) {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  }) : '';
}

2 个答案:

答案 0 :(得分:0)

要使actionPerformed方法运行,您需要为该对象添加ActionListener。由于您没有添加actionListener,因此永远不会运行actionPerformed方法。

检查有关动作侦听器的Java教程:https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

答案 1 :(得分:0)

要运行actionPerformed方法,需要在相应的对象中添加ActionListener。但是JPanel类没有名为addActionListener的方法。

查看代码,您似乎不需要实现ActionListener接口。你可以摆脱actionPerformed方法。您所要做的就是复制actionPerformed方法中的代码并将它们粘贴到keyPressed方法的if条件中。下面是按预期工作的代码:

package edu.uno.ai.planning.bfs;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Gun extends JPanel implements KeyListener{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final int GUN_WIDTH = 100;
    private static final int GUN_HEIGHT = 30;
    private static final int GUN_UPPER_HEIGHT = 20;
    private static final int GUN_UPPER_WIDTH = GUN_WIDTH / 4;
    int x, y;
    int velX;

    public Gun() {
        x = 250 - GUN_WIDTH / 2;
        y = 500 - GUN_HEIGHT;
        velX = 0;
        addKeyListener(this);

        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        int upperX = x + GUN_WIDTH / 2 - GUN_UPPER_WIDTH / 2;
        int upperY = y - GUN_UPPER_HEIGHT;

        g2d.setColor(Color.BLACK);
        g2d.fillRect(x, y, GUN_WIDTH, GUN_HEIGHT);
        g2d.fillRect(upperX, upperY, GUN_UPPER_WIDTH, GUN_UPPER_HEIGHT);
    }

    public void left() {
        velX = -1;
    }

    public void right() {
        velX = 1;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT) {
            left();
            System.out.println("LEFT");
            x += velX;
            repaint();
        }
        if (key == KeyEvent.VK_RIGHT) {
            right();
            System.out.println("RIGHT");
            x += velX;
            repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
    }

    public static void main(String[] args) {
        Gun gun = new Gun();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Game");
        frame.setLayout(new FlowLayout());

        gun.setPreferredSize(new Dimension(500, 500));

        frame.add(gun);
        frame.setVisible(true);
        frame.pack();
    }

}