2012-11-16 48 views
0

我使用Piccolo2D庫在畫布上繪製節點。我在Piccolo2D的在例如已經設置PSelectionEventHandler,如:SelectionExample.java ,然後我在PSelectionEventHandler不觸發回調

private void nodeSelected(final PNotification n) 
{ 
} // nodeSelected 

設置斷點,但是當我選擇的節點,回調不調用。爲什麼?

編輯:(從OP的回答)

好,這裏是代碼:

在主要的JFrame:

this.panelMain().panelWorkspace().canvas().addInputEventListener(
this.handlerCanvasSelection()); 
this.panelMain().panelWorkspace().canvas().getRoot().getDefaultInputManager(). 
    setKeyboardFocus(this.handlerCanvasSelection()); 

PNotificationCenter.defaultCenter().addListener(this, "nodeSelected", 
       PSelectionEventHandler.SELECTION_CHANGED_NOTIFICATION, 
       this.handlerCanvasSelection()); 

private void nodeSelected(final PNotification n) 
{ 
} // nodeSelected 
+0

您必須在其上你可以設置一個斷點方法的聲明。 – Kai

+0

是的,我知道,我有System.print.out(「節點選擇」)裏面,這是錯字錯誤... – KernelPanic

+0

請編輯您的問題,包括[sscce](http://sscce.org/ )顯示你使用'addInputEventListener'。 – trashgod

回答

1

的問題是,nodeSelected()方法是註冊爲回調聲明privatePNotificationCenter.addListener()的實現使用反射來查找並註冊回調方法。它實際上使用Class.getMethod(),它只返回公共方法。所以如果找不到方法(無論什麼原因),監聽器沒有註冊。

考慮這個簡單的例子演示SELECTION_CHANGED_NOTIFICATION通知:

enter image description here

import java.awt.*; 
import javax.swing.*; 
import edu.umd.cs.piccolo.*; 
import edu.umd.cs.piccolo.nodes.*; 
import edu.umd.cs.piccolox.event.*; 

public class TestSelectHandle { 

    private static void createAndShowUI() { 
     JFrame frame = new JFrame("TestSelectHandle"); 
     PCanvas canvas = new PCanvas() { 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(100, 200); 
      } 
     }; 

     final JTextArea output = new JTextArea(5, 20); 

     JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
       canvas, output); 
     frame.add(splitPane); 

     final PNode blueRect = PPath.createRectangle(50, 50, 50, 50); 
     blueRect.setPaint(Color.BLUE); 
     canvas.getLayer().addChild(blueRect); 

     final PNode redRect = PPath.createRectangle(110, 110, 50, 50); 
     redRect.setPaint(Color.RED); 
     canvas.getLayer().addChild(redRect); 

     canvas.removeInputEventListener(canvas.getPanEventHandler()); 
     canvas.removeInputEventListener(canvas.getZoomEventHandler()); 

     PSelectionEventHandler selectionHandler = new PSelectionEventHandler(
       canvas.getLayer(), canvas.getLayer()); 
     canvas.addInputEventListener(selectionHandler); 
     canvas.getRoot().getDefaultInputManager() 
       .setKeyboardFocus(selectionHandler); 

     PNotificationCenter.defaultCenter().addListener(
       new NodeSelectionListener(output), "selectionChanged", 
       PSelectionEventHandler.SELECTION_CHANGED_NOTIFICATION, 
       selectionHandler); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static class NodeSelectionListener { 
     private JTextArea output; 

     public NodeSelectionListener(JTextArea output) { 
      this.output = output; 
     } 

     public void selectionChanged(final PNotification notfication) { 
      output.append("selection changed\n"); 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowUI(); 
      } 
     }); 
    } 
}