0

JPopupMenu我需要在 World Wind 显示中添加右键单击。World Wind 显示在JPanel. 我几乎只是ApplicationTemplate.AppPanel从 World Wind 的示例ApplicationTemplate类中复制了内部类的成员变量和方法,将其粘贴到我需要 WW 显示的 GUI 中,并将this.add(component)复制代码的引用更改为myJPanel.add(component).

除了缺少弹出菜单之外,它工作得很好;我在我的应用程序中嵌入了一个 World Wind 显示器,并从我的应用程序对话框中控制它。

加入JPopupMenu世界风显示后JPanel,好像根本不显示。我右键单击,没有弹出任何内容。我不认为这是隐藏菜单的重量级与轻量级 Java 组件问题,因为我可以将菜单附加到 World Wind 显示上方的组件(WWD 位于BorderLayoutCENTER,其他组件位于其 NORTH)而不是菜单会很高兴地进入世界风展示的空间而不被它隐藏。为了安全起见,我在我所做的主类的静态初始化程序顶部设置了JPopupMenu's和setLightWeightPopupEnabled(false)JPopupMenu.setDefaultLightWeightPopupEnabled(false)

我做了一个MouseListener附加到JPanel包含世界风显示的测试,并且没有任何MouseListener事件被触发。所以我最好的猜测是我不应该将其添加JPopupMenuJPanelwwd 对象的某些特定子组件中,而是应该将其添加到 wwd 对象的某些特定子组件中。wwd 对象本身似乎没有添加弹出菜单的方法,并且我在 wwd 的方法中没有看到类似“getGLCanvas”的东西。如果我在正确的轨道上,应该将菜单添加到哪个组件,以及如何访问该组件?

所以我的问题很简单,或者看起来是这样:我如何JPopupMenu进入 World Wind 显示器?

其次,这个问题也与MouseListener在显示器上显示 a 相同,但我认为这个问题的答案将超出JPopupMenu在显示器上显示 a 的答案。

下面是我插入的 World Wind 模板代码,以及我对它的修改。其他地方的另一个类使用getComponent()JPanel包含 World Wind 显示添加到我的应用程序的用户界面。我留下了我注释掉的默认 World Wind 内容,以防万一这很重要。通过图层名称的 String[] 循环只是我轻松仅显示地图和单位比例的一种方式。JPopupMenu代码位于构造函数的一半。为凌乱的代码道歉,但我认为您应该按原样查看它以获得最佳帮助。

class MyClass
{

protected JComponent component;
public JComponent getComponent() { return component; }

protected WorldWindow wwd;
protected StatusBar statusBar;
protected ToolTipController toolTipController;
protected HighlightController highlightController;

MyClass()
{
    boolean includeStatusBar = false;
    component = new JPanel(new BorderLayout());

    this.wwd = this.createWorldWindow();
    ((Component) this.wwd).setPreferredSize(new Dimension(200,200));//canvasSize);

    // Create the default model as described in the current worldwind properties.
    Model m = (Model) WorldWind.createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
    this.wwd.setModel(m);

    // Setup a select listener for the worldmap click-and-go feature
//            this.wwd.addSelectListener(new ClickAndGoSelectListener(this.getWwd(), WorldMapLayer.class));

    component.add((Component) this.wwd, BorderLayout.CENTER);
    if (includeStatusBar)
    {
        this.statusBar = new StatusBar();
        component.add(statusBar, BorderLayout.PAGE_END);
        this.statusBar.setEventSource(wwd);
    }

    // Add controllers to manage highlighting and tool tips.
//            this.toolTipController = new ToolTipController(this.getWwd(), AVKey.DISPLAY_NAME, null);
//            this.highlightController = new HighlightController(this.getWwd(), SelectEvent.ROLLOVER);

    java.util.List<String> desiredLayers = Arrays.asList(
        new String[] { "Blue Marble May 2004", /*"i-cubed Landsat",*/ "Scale bar"
        });
    for(Layer layer : getWwd().getModel().getLayers())
    {
        if(desiredLayers.contains( layer.getName() ))
        {
            System.out.println("INCLUDE " + layer.getName());
            layer.setEnabled(true);
        }
        else
        {
            System.out.println("EXCLUDE " + layer.getName());
            layer.setEnabled(false);
        }
    }


    JMenu menuZoom = new JMenu("Zoom");
    JMenuItem menuZoom_1028 = new JMenuItem("1028");
    menuZoom.add(menuZoom_1028);
    JMenuItem menuZoom_512 = new JMenuItem("512");
    menuZoom.add(menuZoom_512);
    JMenuItem menuZoom_256 = new JMenuItem("256");
    menuZoom.add(menuZoom_256);
    JMenuItem menuZoom_128 = new JMenuItem("128");
    menuZoom.add(menuZoom_128);
    JMenuItem menuZoom_64 = new JMenuItem("64");
    menuZoom.add(menuZoom_64);
    JMenuItem menuZoom_32 = new JMenuItem("32");
    menuZoom.add(menuZoom_32);
    JPopupMenu rclickMenu = new JPopupMenu();
    rclickMenu.add(menuZoom);
    component.setComponentPopupMenu(rclickMenu);

    menuZoom.getPopupMenu().setLightWeightPopupEnabled(false);

    component.addMouseListener(new MouseListener()
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            System.out.println("mouseClicked");
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
            System.out.println("mousePressed");
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {
            System.out.println("mouseReleased");
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
            System.out.println("mouseEntered");
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
            System.out.println("mouseExited");
        }
    });
}

protected WorldWindow createWorldWindow()
{
    return new WorldWindowGLCanvas();
}

public WorldWindow getWwd()
{
    return wwd;
}

public StatusBar getStatusBar()
{
    return statusBar;
}
}
4

1 回答 1

1

经过大量的挖掘,我终于想出了一个解决方法。

首先,我将解决关于GLPanel从 World Wind wwd 对象获取 wwd 对象的愚蠢子问题:wwd 对象一个GLPanel. 好吧,从技术上讲,WorldWindow是一个接口,但在ApplicationTemplate我从中绘制的 World Wind 应用程序的情况下,它的实现WorldWindow是一个扩展的类GLPanel。我有一阵子没有注意到这一点。

如问题所述,应用于JPanel包含 World Wind 显示的鼠标侦听器不会触发我在地图上的鼠标操作,并且 World Wind 显示没有将鼠标侦听器添加到自身的方法。我终于意识到 World Window 有一个getInputHandler()返回一个对象,您可以将鼠标侦听器添加到该对象。所以我不得不做wwd.getInputHandler().addMouseListener(myMouseAdapter);

正如我所相信的JPopupMenu那样,世界风显示并没有隐藏它。它只是没有被触发弹出。通过上述针对鼠标侦听器的修复,我通过自己打开弹出窗口而不是依赖 Swing 的内置右键单击JPopupMenu支持来解决此问题。我通过jPopupMenu.show(component, event.getX(), event.getY());从鼠标侦听器中调用来做到这一点。

这是我为弹出菜单所做的代码(在 World Wind 的初始化代码中):

    wwd.getInputHandler().addMouseListener(new MouseAdapter()
    {
        @Override
        public void mouseClicked(MouseEvent event)
        {
            // right-click context-menu
            if(event.getButton() == MouseEvent.BUTTON3)
            {
                // component is my JPanel which contains the WorldWindow
                rclickMenu.show(component, event.getX(), event.getY());
            }
        }
    });

我还添加了一些我需要的东西,我会提到它以防万一。我添加了一个检查,以确保右键单击的点专门位于世界地图上,并且我还想知道稍后右键单击的点,因为某些菜单项用于与右键单击的任何内容进行交互。

在这些添加之后,代码如下所示:

    wwd.getInputHandler().addMouseListener(new MouseAdapter()
    {
        @Override
        public void mouseClicked(MouseEvent event)
        {
            // right-click context-menu
            if(event.getButton() == MouseEvent.BUTTON3)
            {
                Position p = wwd.getCurrentPosition();
                // abort if not on-map
                if(p == null) return;

                // need later to know what was right-clicked on
                mPositionAtRightClickMoment = wwd.getCurrentPosition();

                rclickMenu.show(component, event.getX(), event.getY());
            }
        }
    });
于 2016-11-29T14:11:57.400 回答