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 位于BorderLayout
CENTER,其他组件位于其 NORTH)而不是菜单会很高兴地进入世界风展示的空间而不被它隐藏。为了安全起见,我在我所做的主类的静态初始化程序顶部设置了JPopupMenu
's和setLightWeightPopupEnabled(false)
JPopupMenu.setDefaultLightWeightPopupEnabled(false)
我做了一个MouseListener
附加到JPanel
包含世界风显示的测试,并且没有任何MouseListener
事件被触发。所以我最好的猜测是我不应该将其添加JPopupMenu
到JPanel
wwd 对象的某些特定子组件中,而是应该将其添加到 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;
}
}