本文介绍了JMapViewer,MouseListener调用了2次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 使用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 中,在 /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 或 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次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 13:46