mouseMoved工作时mouseClicked不起作用

时间:2015-02-01 16:50:01

标签: image 3d onclick mouseevent mouse

所以我有一个我工作的新项目,我在一个房间里移动(全景图)。现在我想在mouseClick上打开一些东西。但是,当我尝试在mouseClick事件中向控制台打印某些内容时,它无效。

package noam;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

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


public class room extends JPanel{
    ImageIcon img;
    int locationX=0,locationY=0;
    int cp=0,cr=0,c=0;//cp clickpresed cr clickreleased c clicked

    public room()
    {
        addMouseMotionListener(new MML());
        //addMouseMotionListener(new ML());
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);        
        img =new ImageIcon("VRpano.jpg");
        Image s=img.getImage();
        g.drawImage(s, locationX,locationY,img.getIconWidth(),img.getIconHeight(), null);
    }




    class MML extends MouseAdapter{

        /*public void mousePressed(MouseEvent e) {

        }*/

        public void mouseMoved(MouseEvent e) {

            if(e.getX()>1100){
                if(locationX>-6350)
                    locationX-=20;
                if(locationX<=-6350)
                    locationX=0;
                repaint();
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

            if(e.getX()<200){
                if(locationX<0)
                    locationX+=20;
                if(locationX>=0)
                    locationX=-6300;
                repaint();
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

            //System.out.println("x="+e.getX()+" y="+e.getY());
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            super.mouseClicked(e);
            System.out.println("click!");
            c++;
            System.out.println(c);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            super.mousePressed(e);
            System.out.println("Pressed!");
            cp++;
            System.out.println(cp);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
            super.mouseReleased(e);
            System.out.println("Released!");
            cr++;
            System.out.println(cr);
        }

    }
    /*public class ML extends MouseAdapter{
        public void MOUSE_ENTERED(MouseEvent me){
            System.out.println("click!");

        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }*/

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame f=new JFrame("Asteroids MS Ver 1 (c) 2014");
        room bp=new room();
        f.add(bp);
        f.setSize(1650,1080);
        f.setExtendedState(JFrame.MAXIMIZED_BOTH); 
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        f.setVisible(true);

    }


}

0 个答案:

没有答案