为了更好地理解布局算法在Jgraphx中的工作原理,我开发了一个示例来展示我的问题。
1)在第一个例子中,我创建了一个连接了两个节点的节点,然后应用布局算法,所有工作都按预期工作,因为我用作所有节点的父节点和边缘默认父节点
```
package it.test.graphDisplay;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
public class GraphDisplayTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setBounds(0, 0, 400, 300);
mxGraph graph = buildGraph();
mxGraphComponent graphComponent = new mxGraphComponent(graph);
frame.getContentPane().add(graphComponent);
frame.setVisible(true);
}
private mxGraph buildGraph() {
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
Object v1 = graph.insertVertex(parent, "V1", "V1", 20, 20,
80, 30);
Object v2 = graph.insertVertex(parent, "V2", "V2", 240,
150, 80, 30);
graph.insertEdge(parent, "E1", "E1", v1, v2);
Object v3 = graph.insertVertex(parent, "V3", "V3", 240,
200, 80, 30);
graph.insertEdge(parent, "E2", "E2", v1, v3);
// apply layout to graph
mxHierarchicalLayout layout = new mxHierarchicalLayout(
graph);
layout.setOrientation(SwingConstants.WEST);
layout.execute(parent);
} finally {
graph.getModel().endUpdate();
}
return graph;
}
});
}
}
2)在第二个例子中,我首先使用第二个和第三个节点的父节点,然后应用布局。结果如下
package it.test.graphDisplay;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
public class GraphDisplayTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setBounds(0, 0, 400, 300);
mxGraph graph = buildGraph();
mxGraphComponent graphComponent = new mxGraphComponent(graph);
frame.getContentPane().add(graphComponent);
frame.setVisible(true);
}
private mxGraph buildGraph() {
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
Object v1 = graph.insertVertex(parent, "V1", "V1", 20, 20,
80, 30);
Object v2 = graph.insertVertex(v1, "V2", "V2", 240, 150,
80, 30);
graph.insertEdge(v1, "E1", "E1", v1, v2);
Object v3 = graph.insertVertex(v1, "V3", "V3", 240, 200,
80, 30);
graph.insertEdge(parent, "E2", "E2", v1, v3);
// apply layout to graph
mxHierarchicalLayout layout = new mxHierarchicalLayout(
graph);
layout.setOrientation(SwingConstants.WEST);
layout.execute(parent);
} finally {
graph.getModel().endUpdate();
}
return graph;
}
});
}
}
这种行为有这个原因吗?我无法在文档中找到任何解释。
谢谢!
p.s:这里也开了一个问题:https://github.com/jgraph/jgraphx/issues/50到目前为止没有线索