JPanel没有收听MouseAdapter

时间:2012-11-26 10:18:27

标签: java jpanel mouselistener

我有这个问题。我必须做一个益智游戏。这有一个主要的JPanel(PPal),它有一个BorderLAyout。在北方它有另一个JPanel(PPiezas),它有各种各样的拼图。我有两个鼠标事件,一个(ClickSobrePieza)与片段面板上的点击一起工作以选择一个片段。 这个没有问题。然后我在主面板上添加了另一个鼠标事件(RatonMueve),它应该监听每个鼠标的动作,如果有一个selectec部分,当你移动鼠标时,将它移动到主面板上。这里的问题是这个事件不起作用。它甚至没有输入代码。我尝试在PPal面板上使用setFocusable()和requestFocusable(),但它没有任何效果。任何人都知道为什么会这样?

package GUI;

import data.Pieza;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JLayeredPane;
import javax.swing.event.MouseInputAdapter;

/**
*
 * @author rootmobil
 */
public class PPal extends JLayeredPane{
private VPal v;//that's JFrame, it contains this panel
private PPiezas pPiezas;//thats the JPanel in the NORTH
private PHuecos pHuecos;//that's a JPanel in CENTRE
private Pieza piezaMostrada;//that;s the piece should be showed and moved, It't the piece user choose
private Point p;//that's the point that will containt the mouse pointer position and will mark where must be piezaMostrada each time

public PPal(VPal v) {
    this.v=v;

    this.setFocusable(true);
    this.requestFocus();
    this.addMouseMotionListener(new RatonMueve());

    this.setLayout(new BorderLayout());

    this.pPiezas=new PPiezas(v);
    this.pHuecos=new PHuecos(v);

    this.moveToBack(this.pPiezas);
    this.moveToBack(this.pHuecos);

    this.pPiezas.addMouseListener(new ClickSobrePieza());
    this.add(pPiezas,BorderLayout.NORTH,1);
    this.add(new PHuecos(v),BorderLayout.CENTER,1);

}

   class RatonMueve extends MouseInputAdapter{
    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("algo");//I put that to see if program enter into this code, it never does
        if (v.getJuego().getPiezaAMostrar()!=null) {
            piezaMostrada=new Pieza(v.getJuego().getPiezaAMostrar().getImgPath(),v.getJuego().getPiezaAMostrar().getPosicion());//that creates piezaMostrada from the selected one in the north panel
            PPal.this.moveToFront(piezaMostrada);
            piezaMostrada.setLocation(p.x,p.y);
            PPal.this.add(piezaMostrada);
        }

    }
    @Override
    public void mouseMoved(MouseEvent e) {
        p=new Point(e.getX(),e.getY());
        if (piezaMostrada!=null) {
            piezaMostrada.setLocation(p.x,p.y);
            PPal.this.repaint();
        }
    }
}

   //this is where i take the selected piece by the user, this code works perfect
   class ClickSobrePieza extends MouseAdapter{
    Point p;

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        p=e.getPoint();

        if (e.getComponent().getComponentAt(p).getClass()==Pieza.class){
            if (v.getJuego().getPiezaSeleccionada()!=null) {
               v.getJuego().getPiezaSeleccionada().setEnabled(true); 
            }
            v.getJuego().setPiezaSeleccionada((Pieza)e.getComponent().getComponentAt(p));
            v.getJuego().setPiezaAMostrar(new Pieza(v.getJuego().getPiezaSeleccionada().getImgPath(),
                                                        v.getJuego().getPiezaSeleccionada().getPosicion()));

            v.getJuego().getPiezaSeleccionada().setEnabled(false);
        }
    }


}

   //draws a background image 
   @Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(new ImageIcon("./res/fondo.jpg").getImage(),0,0,this.getWidth(),this.getHeight(),null);
}

}//:)

0 个答案:

没有答案