Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在匿名类侦听器中添加MouseeEvent?_Java_Swing - Fatal编程技术网

Java 如何在匿名类侦听器中添加MouseeEvent?

Java 如何在匿名类侦听器中添加MouseeEvent?,java,swing,Java,Swing,所以我在JFrame中做了一个益智游戏,但是我不知道如何使用MouseeEvent以及在匿名类侦听器中放置它的位置。 这就是我的问题。 我需要将图像移动到帧的中心,猜猜图像是什么 //p5 components(continue frame) ImageIcon pic1st = new ImageIcon("C:\\java pics\\w.png"); JLabel pic1st0 = new JLabel(pic1st); JLabel level = ne

所以我在JFrame中做了一个益智游戏,但是我不知道如何使用MouseeEvent以及在匿名类侦听器中放置它的位置。 这就是我的问题。 我需要将图像移动到帧的中心,猜猜图像是什么

    //p5 components(continue frame)
    ImageIcon pic1st = new ImageIcon("C:\\java pics\\w.png");
    JLabel pic1st0 = new JLabel(pic1st);
    JLabel level = new JLabel("Level:" + l);
    JLabel score = new JLabel("Score:" + s);
    JLabel time = new JLabel("Time:" + t);
和匿名类侦听器

//这是我代码的一部分,我想把MouseEvent放在这里

continue1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            f.add(p5);
            f.remove(p20);
            f.setVisible(true);
            f.revalidate();
            f.repaint();
        }
    });
//Where to put the MouseEvent here?

任何帮助都将不胜感激

为什么这里需要鼠标事件?您需要在鼠标下放置图像吗?你说你正在居中…无论如何,正确跟踪鼠标事件的方法是在需要跟踪鼠标事件的组件中添加一个
MouseAdapter
MouseListener
。请看。

我的意思是,图像的某些部分随机放置在帧中,然后我需要将其放在中间,以查看图片内容,然后猜测它。所以可以在匿名类侦听器中添加MouseEvent?否-MouseListener是需要添加到组件的独立类-与添加ActionListener的方式相同。不过,如果您需要跟踪鼠标的实际位置或跟踪非按钮组件上的单击,您只需要一个鼠标听筒。要更快获得更好的帮助,请发布一个。有大量文档,包括教程和工作示例!我认为
ActionListener
MouseListener
无关,只是它们共享超类
EventListener
。将鼠标事件处理程序添加到要监视的组件(框架或面板)。我会在这里开始的