0

我正在尝试实现单击和拖动方法,为此我需要 mousePressed 和 mouseReleased 事件。我还将使用 mouseClicked 事件,因此我实现了 MouseMotionListener 和 MouseListener。

但是,当我编写 mousePressed、mouseReleased、mouseEntered、mouseExited 方法时,出现以下错误:

method mousePressed(MouseEvent) is already defined in class BoingPanel

这是整个班级:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JPanel;

public class BoingPanel extends JPanel implements MouseMotionListener, MouseListener {

private int width;
private int height;

private int updateRate=40;

ArrayList<Ball> balls;
ArrayList<Line> lines;

private Container box;

private boolean drag = false;

public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }

public BoingPanel()
{

    balls = new ArrayList<Ball>();

    balls.add(new Ball(100,0));  // adds test ball
    balls.get(0).setXVelocity(5);
    balls.add(new Ball(100,0));
    balls.add(new Ball(400,50));

    lines = new ArrayList<Line>();
    lines.add(new Line(0, height, width, height));


    gameStart();
}

public BoingPanel(int x, int y)
{
    width=x;
    height=y;


    balls = new ArrayList<Ball>();

    balls.add(new Ball(100,0));  // adds test ball
    balls.get(0).setXVelocity(5);
    balls.add(new Ball(100,0));
    balls.add(new Ball(400,50));

    lines = new ArrayList<Line>();


    box = new Container(width, height);


    gameStart();
}

@Override
public void mouseDragged(MouseEvent me) {
}

@Override
public void mouseMoved(MouseEvent e) {
}

@Override
public void mouseClicked(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

 public void gameStart() {

  // Run the game logic in its own thread.
  Thread gameThread = new Thread() {

     public void run() {
        while (true) {
           // Execute one time-step for the game 
           update();
           // Refresh the display
           repaint();
           // Delay and give other thread a chance
           try {

              Thread.sleep(1000 /updateRate);

           } catch (InterruptedException ex) {}
        }
     }
  };
  gameThread.start();  // Invoke GaemThread.run()
}

private void update()
{
    for(Ball a:balls)  //calls balls updated position
    {
        a.update(box);


    }

}

public void paintComponent(Graphics g)
{
    box.paint(g);

    for(Ball a:balls)  //calls balls updated position
    {
        a.paint(g);
    }

    for(Line b:lines)  //draws lines
    {
        b.paint(g);
    }

}

public void actionPerformed(ActionEvent e)
{
    update(); //changes ball position
    repaint(); //refreshes image
}

}

是什么导致了这个错误?谢谢!

4

2 回答 2

2

我建议在您的变量声明下方和构造函数之前这样做,没有帮助......

public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }

您基本上已经两次实现了这些方法。就在构造函数之前和之后。

只需删除这些声明组...

于 2013-05-28T04:15:22.433 回答
1

请删除public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { }然后尝试

于 2013-05-28T04:17:46.550 回答