【发布时间】: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,其他组件位于其北部)并且菜单将愉快地进入世界风显示的空间而不会被它隐藏。为了安全起见,我设置了JPopupMenu 的setLightWeightPopupEnabled(false),并在主类的静态初始化器顶部设置了JPopupMenu.setDefaultLightWeightPopupEnabled(false)
我使用附加到包含 World Wind 显示的 JPanel 的 MouseListener 进行了测试,并且没有触发任何 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;
}
}
【问题讨论】: