gpt4 book ai didi

java - 在顶点上添加鼠标监听器

转载 作者:行者123 更新时间:2023-12-01 11:00:21 25 4
gpt4 key购买 nike

我正在尝试使用 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");
}

}
}
});

最佳答案

你的代码对我来说工作得很好。试试这个,它应该 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);
}
}

关于java - 在顶点上添加鼠标监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33387828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com