简体   繁体   English

JMapViewer,MouseListener调用了2次

[英]JMapViewer, MouseListener called 2 times

Working with JMapViewer, a strange behavior of the component was recognized. 使用JMapViewer,可以识别组件的异常行为。 I am using DefaultMapController to get the map position (lat, lon). 我正在使用DefaultMapController来获取地图位置(纬度,经度)。

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);
    }
}

Running the code, after the left mouse button is pressed, the method mouseClicked() gets called multiple times (2x). 运行代码,在按下鼠标左键后,方法mouseClicked()被调用多次(2x)。 After the replacement 更换后

    addMouseListener(new DefaultMapController(this) {

with

    addMouseListener(new MouseAdapter() {

the code works correctly, the method gets called only 1x. 代码正常工作,该方法仅被调用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. 谢谢你的帮助。

Your Test extends JMapViewer , adding a MouseListener in an instance initializer block. 您的Test扩展了JMapViewer ,在实例初始化器块中添加了MouseListener 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. 超类, JMapController ,将您MouseListener -你猜对了, 第二次。

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

Instead, create a new JMapController or DefaultMapController , as shown here , and use it to construct your JMapViewer . 相反,创建一个新的JMapControllerDefaultMapController ,如图所示这里 ,并用它来构建你的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 https://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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM