2016-11-28 30 views
0

我需要在世界風顯示屏上添加一個右擊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顯示屏的空間而不會被隱藏。爲了安全起見,我設置了JPopupMenusetLightWeightPopupEnabled(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; 
} 
} 

回答

0

經過大量的挖掘,我終於想出了一個解決方法。

首先,我將我的地址傻子問題有關從世界風WWD對象獲取GLPanel:在WWD對象一個GLPanel。嗯,從技術上講,WorldWindow是一個接口,但是在我繪製的World Wind ApplicationTemplate應用程序中,WorldWindow的實現是GLPanel的一個類。我沒有注意到這一段時間。

如上所述,適用於包含World Wind顯示的JPanel的鼠標偵聽器不會在地圖上觸發我的鼠標操作,並且World Wind顯示沒有向其自身添加鼠標偵聽器的方法。我終於意識到World Window有一個getInputHandler(),它返回一個可以添加鼠標偵聽器的對象。所以我不得不做wwd.getInputHandler().addMouseListener(myMouseAdapter);

據我所知,JPopupMenu沒有被世界風顯示隱藏;它只是沒有被觸發彈出。對於鼠標聽衆的上述修復,我通過自己打開彈出窗口來補救此問題,而不是依靠Swing的內置右鍵單擊JPopupMenu支持。我通過在鼠標監聽器中調用jPopupMenu.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) 
      { 
       // 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()); 
      } 
     } 
    });