gpt4 book ai didi

java - JGraphX 图中边不可见

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

我正在研究 JGraphX。我使用 graphComponent 的 insertEdge 方法在两个顶点之间添加了一条边。但边缘不可见。如果我保存,然后关闭,然后打开同一个文件,我可以看到边缘。为什么我添加后看不到?

如果我为同一单元格创建一条边,即 graph.insertEdge(parent,null, "hasEntity", newCell, newCell),那么它会将边添加到自身,即源顶点和目标顶点相同。我不需要在这里关闭和打开图表。

mxCell defaultActivityEdge = (mxCell) graph.insertEdge(parentObject, null, "hasActivity", cell3, newCell); 
graph.addCell(defaultActivityEdge);
defaultActivityEdge.setVisible(true);
graph.clearSelection();
graph.refresh()

最佳答案

您不需要将 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();
}
}

输出

enter image description here

关于java - JGraphX 图中边不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24380194/

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