0

我正在尝试用 Java 中的 JGraphX 制作一个图形,并mouseListener在特定类型的顶点上添加一个。

我试图使用graphComponent,但它什么也没做......

这是我的代码:

        mxGraphComponent graphComponent = new mxGraphComponent(graph);

        graphComponent.getGraphControl().addMouseListener( new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                mxCell cell = (mxCell) graphComponent.getCellAt(e.getX(), e.getY());
                System.out.println("TOTO");

                if(cell != null && cell instanceof mxCell){
                    if(cell.getValue().toString().equals("AAM")){
                        System.out.println("My Cell selected");
                    }

                }
            }
        });
4

1 回答 1

1

你的代码对我来说很好。试试这个,它应该 100% 工作:

public class HelloWorld extends JFrame
{
    private static final long serialVersionUID = -2707712944901661771L;

    public HelloWorld()
    {
        super("Hello, World!");

        mxGraph graph = new mxGraph();
        Object parent = graph.getDefaultParent();

        graph.getModel().beginUpdate();
        try
        {
            String s1 = "Hello";
            String s2 = "World!";
            Object v1 = graph.insertVertex(parent, "ID1", s1, 20, 20, 80, 30);
            Object v2 = graph.insertVertex(parent, "ID2", s2, 240, 150, 80, 30);
            Object edge = graph.insertEdge(parent, "ID3", "TEST", v1, v2);
        }
        finally
        {
            graph.getModel().endUpdate();
        }
        final mxGraphComponent graphComponent = new mxGraphComponent(graph);

        graphComponent.getGraphControl().addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                Object cell = graphComponent.getCellAt(e.getX(), e.getY());
                if (cell instanceof mxCell)
                {
                    System.out.println(((mxCell) cell).getValue());
                }
            }
        });
        getContentPane().add(graphComponent);
    }

    public static void main(String[] args)
    {
        HelloWorld frame = new HelloWorld();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 320);
        frame.setVisible(true);
    }
}
于 2015-10-28T13:36:51.477 回答