Java PSelectionEventHandler不触发回调

Java PSelectionEventHandler不触发回调,java,swing,piccolo,Java,Swing,Piccolo,我正在使用Piccolo2D库在画布上绘制节点。我在Piccolo2D的示例中设置了PSelectionEventHandler,如下所示: 然后我在中设置断点 private void nodeSelected(final PNotification n) { } // nodeSelected 但当我选择节点时,不会调用回调。为什么? 编辑:(摘自OP的答案) 好的,代码如下: 在主框架中: this.panelMain().panelWorkspace().canvas().addI

我正在使用Piccolo2D库在画布上绘制节点。我在Piccolo2D的示例中设置了PSelectionEventHandler,如下所示: 然后我在中设置断点

private void nodeSelected(final PNotification n)
{
}   // nodeSelected
但当我选择节点时,不会调用回调。为什么?

编辑:(摘自OP的答案)

好的,代码如下:

在主框架中:

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

问题是注册为回调的
nodeSelected()
方法被声明为
private
PNotificationCenter.addListener()的实现使用反射来查找和注册回调方法。它实际上使用只返回公共方法的
Class.getMethod()
。因此,如果找不到该方法(无论出于何种原因),侦听器将不会注册

考虑这个演示
选择\u更改\u通知的简单示例
通知:

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();
            }
        });
    }
}


您必须在可以设置断点的方法中有一条语句。是的,我知道,我在其中有System.print.out(“已选择节点”),这是打字错误…请编辑您的问题,以包含一条显示您使用
addInputEventListener
。好的,下面是代码:在主JFrame中:this.panelMain().panelWorkspace().canvas().addInputEventListener(this.handlerCanvasSelection());this.panelMain().panelWorkspace().canvas().getRoot().getDefaultInputManager().setKeyboardFocus(this.handlerCanvasSelection());PNotificationCenter.defaultCenter().addListener(此为“nodeSelected”,PSelectionEventHandler.SELECTION\u已更改\u通知,此为.handlerCanvasSelection());private void nodeSelected(final PNotification n){}//nodeSelected是我犯错误的地方