我需要在世界風顯示屏上添加一個右擊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;
}
}