0

キャンバスにノードを描画するためにPiccolo2Dライブラリを使用しています。Piccolo2Dの例のようにPSelectionEventHandlerを次の場所に設定しました:SelectionExample.java 次に、ブレークポイントを設定します

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
4

1 に答える 1

1

問題はnodeSelected()、コールバックとして登録されているメソッドが宣言されていることですprivate。の実装でPNotificationCenter.addListener()は、リフレクションを使用してコールバックメソッドを検索および登録します。実際にはClass.getMethod()、パブリックメソッドのみを返すを使用します。したがって、(何らかの理由で)メソッドが見つからない場合、リスナーは登録されません。

SELECTION_CHANGED_NOTIFICATION通知を示すこの簡単な例を考えてみましょう。

ここに画像の説明を入力してください

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();
            }
        });
    }
}
于 2012-12-21T04:44:23.477 に答える