JMapViewer,MouseListener调用了2次 [英] JMapViewer, MouseListener called 2 times

查看:232
本文介绍了JMapViewer,MouseListener调用了2次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JMapViewer,可以识别组件的一个奇怪的行为。我使用DefaultMapController来获取地图位置(lat,lon)。

Working with JMapViewer, a strange behavior of the component was recognized. I am using DefaultMapController to get the map position (lat, lon).

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import org.openstreetmap.gui.jmapviewer.DefaultMapController;
import org.openstreetmap.gui.jmapviewer.JMapViewer;

public class Test extends JMapViewer{

public Test() 
{
    addMouseListener(new DefaultMapController(this) {
            public void mouseClicked(MouseEvent e){
            Point  start = e.getPoint();
            System.out.println(e.getPoint());
            }            
       });
  }

protected void paintComponent(Graphics g){super.paintComponent(g);}  

public static void main (String [] args){
            JFrame jf = new JFrame();
            jf.setSize(800, 600);
            Test t= new Test();
            jf.add(t);
            jf.setVisible(true);
    }
}

按下鼠标左键后运行代码,方法mouseClicked()被调用多次(2x)。替换后

Running the code, after the left mouse button is pressed, the method mouseClicked() gets called multiple times (2x). After the replacement

    addMouseListener(new DefaultMapController(this) {

    addMouseListener(new MouseAdapter() {

代码工作正常,该方法只调用1x。问题在哪里?

the code works correctly, the method gets called only 1x. Where is the problem? Is it a bug inside the library or the syntax is wrong or unsafe? How to avoid this issue? Thanks for your help.

推荐答案

您的语法错误或不安全?测试扩展 JMapViewer 中,在 MouseListener /docs.oracle.com/javase/tutorial/java/javaOO/initial.htmlrel =nofollow> instance initializer block。作为结果,默认构造函数将调用超类的无参构造函数。超类, JMapController 添加您的 MouseListener - 您已经猜到了

Your Test extends JMapViewer, adding a MouseListener in an instance initializer block. As a consequence, the "default constructor will call the no-argument constructor of the superclass." The superclass, JMapController, adds your MouseListener—you guessed it—a second time.

public JMapController(JMapViewer map) {
    this.map = map;
    if (this instanceof MouseListener)
        map.addMouseListener((MouseListener) this);
    …
}


$ b < JMapController
DefaultMapController ,如这里,并使用它来构造 JMapViewer

Instead, create a new JMapController or DefaultMapController, as shown here, and use it to construct your JMapViewer.

import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import org.openstreetmap.gui.jmapviewer.DefaultMapController;
import org.openstreetmap.gui.jmapviewer.JMapViewer;

/**
 * @see http://stackoverflow.com/a/39461854/230513
 */
public class TestMapController {

    private void display() {
        JFrame f = new JFrame("TestMapController");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMapViewer map = new JMapViewer();
        new DefaultMapController(map) {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println(e.getPoint());
            }
        };
        f.add(map);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new TestMapController()::display);
    }
}

这篇关于JMapViewer,MouseListener调用了2次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆