我正在尝试解决需要在 Swing 中显示图形的任务。找到JGraph框架(现在称为 JGraphX)。在示例中,graph.insertVertex(...)
方法用于添加顶点,graph.insertEdge(...)
方法用于添加边 - 可以正常工作。
但是我怎样才能删除那个顶点呢?
最佳答案
看起来您可以使用 mxGraph.removeCells
方法移除顶点。我修改了 HelloWorld JGraphX 中包含的示例(使用版本 3.4.1 ):
import com.mxgraph.swing.*;
import com.mxgraph.view.*;
import javax.swing.*;
/**
* Adapted from https://github.com/jgraph/jgraphx/blob/master/examples
* /com/mxgraph/examples/swing/HelloWorld.java
*/
public class GoodbyeVertex extends JFrame {
private static final long serialVersionUID = -2707712944901661771L;
public static void main(String[] args) {
GoodbyeVertex frame = new GoodbyeVertex();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
public GoodbyeVertex() {
super("Hello, World!");
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80, 30);
Object v2 = graph.insertVertex(parent, null, "World!", 240, 150, 80, 30);
graph.insertEdge(parent, null, "Edge", v1, v2);
// Remove a vertex. The related edge is removed as well.
graph.removeCells(new Object[]{v1});
} finally {
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
}
关于java - 如何删除 JGraphX 库中的顶点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295251/