简体   繁体   English

JMapViewer:setMovementMouseButton()方法的奇怪行为

[英]JMapViewer: strange behavior of setMovementMouseButton() method

I am trying to change the mouse button to pan the map view from the right button to left. 我正在尝试更改鼠标按钮以从右按钮向左平移地图视图。

There is a simple code changing the button after the left mouse button has been clicked: 单击鼠标左键后,有一个简单的代码可以更改按钮:

public class Map extends JMapViewer {

    public Map()         {
            new DefaultMapController(this){
                    public void mousePressed(MouseEvent e) {                   
                            this.setMovementMouseButton(MouseEvent.BUTTON1);
                    }   
            };
    }
}

Main class: 主班:

public class JMapViewerDemo {
    public static void main(String[] args) {
           JFrame f = new JFrame();
           f.add(new Map());
           f.setSize(800, 600);
           f.setVisible(true);          
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Surprisingly, the code does not work (no reassignment has been done). 令人惊讶的是,该代码无法正常工作(未完成重新分配)。 Why? 为什么? However, after calling the parent class method 但是,调用父类方法后

public void mousePressed(MouseEvent e) {    
     super.mousePressed(e);      //Calling the parent-class method          
     this.setMovementMouseButton(MouseEvent.BUTTON1);
}   

the following behavior was observed: 观察到以下行为:

  1. Click the left mouse button. 单击鼠标左键。 When dragging no reassignment has been done (the same situation). 拖动时,未进行任何重新分配(相同的情况)。

  2. Release the left mouse button. 释放鼠标左键。

  3. Click the left mouse button again. 再次单击鼠标左键。 When dragging, the panning is assign to the left mouse button. 拖动时,平移将分配给鼠标左键。

I find this behavior strange. 我发现这种行为很奇怪。 Maybe, I am using this method incorrectly... 也许我使用这种方法不正确...

How to change the panning button directly, without being released and clicked again? 如何直接更改平移按钮,而无需释放并再次单击? Thanks for your help... 谢谢你的帮助...

The results seen are not unexpected, but they may be a little counter-intuitive: Your example alters the internal state of the DefaultMapController in your implementation of mousePressed() . 看到的结果并非出乎意料,但可能有点违反直觉:您的示例实现mousePressed()更改了DefaultMapController的内部状态。 Instead, save a reference to the custom controller so that you can change the preferred button as desired: 而是保存对自定义控制器的引用,以便您可以根据需要更改首选按钮:

    JMapViewer map = new JMapViewer();
    DefaultMapController dmc = new DefaultMapController(map){…};
    dmc.setMovementMouseButton(MouseEvent.BUTTON1);

In a desktop application, you might let the user select a preferred mouse button using radio buttons and save the chosen value among the user's Preferences as shown here . 在一个桌面应用程序,您可以让用户使用单选按钮选择一个首选鼠标按钮并保存用户间的选择价值Preferences如图所示这里 Also consider overriding getPreferredSize() , as shown here , instead of invoking setSize() . 也可以考虑覆盖getPreferredSize()如图所示这里 ,而不是调用setSize()

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

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