我需要在世界风显示屏上添加一个右击JPopupMenu
。世界风显示在JPanel
。我几乎只是复制了World Wind的示例ApplicationTemplate
类中的ApplicationTemplate.AppPanel
内部类的成员变量和方法,将它粘贴到GUI中,我需要WW显示,并且将this.add(component)
引用从复制的代码更改为myJPanel.add(component)
。在世界风显示屏中获取JPopupMenu
除了缺少弹出式菜单外,它工作得很好;我有一个嵌入到我的应用程序中的World Wind显示屏,并从我的应用程序对话框中控制它。
在将世界风力显示屏JPanel
添加到JPopupMenu
之后,它似乎完全不显示。我右键单击,并没有弹出。我不认为这是一个重量级与轻量级Java组件隐藏菜单的问题,因为我可以将菜单附加到世界风显示上方的组件(WWD在BorderLayout
CENTER中,其他组件位于其NORTH),而菜单将会愉快地进入World Wind显示屏的空间而不会被隐藏。为了安全起见,我设置了JPopupMenu
的setLightWeightPopupEnabled(false)
和静态初始化顶我做JPopupMenu.setDefaultLightWeightPopupEnabled(false)
我做了连接到包含世风显示JPanel
一个MouseListener
测试主类的,没有的触发了事件MouseListener
。所以我最好的猜测是我不应该将JPopupMenu
添加到JPanel
,而应该将其添加到wwd对象的某个特定子组件。 wwd对象本身似乎没有添加弹出菜单的方法,在wwd的方法中我没有看到任何像“getGLCanvas”的东西。如果我在这里找到了正确的方向,菜单中应该添加什么组件,以及如何访问该组件?
所以我的问题很简单,或者看起来如此:我如何获得JPopupMenu
到World Wind显示屏上?
其次,这个问题也引出了一个同样获得显示器上的MouseListener
的,但我认为这个问题的答案就会掉出来的答案到显示器上得到一个JPopupMenu
。
下面是我插入的World Wind模板代码,以及对其的修改。另一个类别使用getComponent()
将包含World Wind显示的JPanel
添加到我的应用程序的用户界面。我留下了默认的世界风的东西,我注释到的情况下,以某种方式显着。通过图层名称的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;
}
}