【问题标题】:Getting a JPopupMenu in a World Wind display在 World Wind 显示中获取 JPopupMenu
【发布时间】:2017-04-12 04:10:41
【问题描述】:

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

除了缺少弹出菜单外,它的效果很好;我在我的应用程序中嵌入了 World Wind 显示器,并通过我的应用程序对话框对其进行控制。

在世界风显示的JPanel中添加了JPopupMenu后,似乎根本不显示。我右键单击,没有弹出任何内容。我不认为这是隐藏菜单的重量级与轻量级 Java 组件问题,因为我可以将菜单附加到 World Wind 显示上方的组件(WWD 位于 BorderLayout CENTER,其他组件位于其北部)并且菜单将愉快地进入世界风显示的空间而不会被它隐藏。为了安全起见,我设置了JPopupMenusetLightWeightPopupEnabled(false),并在主类的静态初始化器顶部设置了JPopupMenu.setDefaultLightWeightPopupEnabled(false)

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

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

其次,这个问题也与在显示屏上显示 MouseListener 相同,但我认为该问题的答案将与在显示屏上显示 JPopupMenu 的答案不同。

下面是我插入的 World Wind 模板代码,以及我对它的修改。其他地方的另一个类使用getComponent() 将包含World Wind 显示的JPanel 添加到我的应用程序的用户界面中。我留下了我注释掉的默认 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;
}
}

【问题讨论】:

    标签: java worldwind


    【解决方案1】:

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

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

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

    正如我所相信的,JPopupMenu 并没有被 World Wind 显示隐藏;它只是没有被触发弹出。通过上述针对鼠标侦听器的修复,我通过自己打开弹出窗口而不是依赖 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());
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 1970-01-01
      • 2017-11-29
      • 2013-01-12
      • 2012-07-09
      • 2011-09-24
      • 2015-10-03
      • 2011-08-14
      • 2017-03-03
      相关资源
      最近更新 更多