您无需将 insertEdge() 的返回对象作为单元格添加到图形中。请查看以下示例并在您的代码中进行更正。
public class MxGraphSample {
public static void creategraph() {
final JFrame frame = new JFrame();
frame.setSize(300, 300);
JPanel panel = new JPanel();
panel.setSize(frame.getMaximumSize().width,
frame.getMaximumSize().height);
final mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
Object v1 = graph.insertVertex(parent, null, "v1", 20, 20, 80, 30);
Object v2 = graph.insertVertex(parent, null, "v2", 120, 70, 80, 30);
Object v3 = graph.insertVertex(parent, null, "v3", 220, 70, 80, 30,
"fillColor=lightgreen");
graph.insertEdge(parent, null, "", v1, v2);
graph.insertEdge(parent, null, "", v1, v3, "strokeColor=lightgreen");
graph.cellsFolded(new Object[] {v1, v2, v3}, true, true);
mxCompactTreeLayout layout = new mxCompactTreeLayout(graph);
layout.setLevelDistance(40);
layout.setNodeDistance(30);
layout.setEdgeRouting(false);
layout.setUseBoundingBox(false);
layout.execute(graph.getDefaultParent());
} finally {
graph.getModel().endUpdate();
}
final mxGraphComponent graphComponent = new mxGraphComponent(graph);
graphComponent.setFoldingEnabled(true);
panel.setLayout(new BorderLayout());
panel.add(graphComponent, BorderLayout.CENTER);
frame.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
creategraph();
}
}
输出