gpt4 book ai didi

java - 如何最好地显示 JGraphX?

转载 作者:行者123 更新时间:2023-12-02 08:28:44 31 4
gpt4 key购买 nike

是否可以通过将 JGraphX 添加到 JLable 来显示它?testJGraphX(如下)的大部分内容取自 JGraphX Hello World 示例,但该图未显示在 jLable1 中。对于 JGraphX,是否有比 JLabel 更好的容器?

public class TestJGraphX extends javax.swing.JFrame implements TableModelListener {
public TestJGraphX() {
initComponents();
testJGraphX();
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
...
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
...
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
...
}
private void testJGraphX() {
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);
} finally {
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
jLabel1.add(graphComponent);
}
}

最佳答案

JPanel 应该可以工作,或者直接将其添加到 JFrame 中即可。

编辑 - 下面的代码对我来说显示了很好的图表。

import javax.swing.JFrame;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class GraphFrame extends JFrame {
public static void main(String[] args) {
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);
} finally {
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);

GraphFrame frame = new GraphFrame();
frame.add(graphComponent);
frame.pack();
frame.setVisible(true);
}
}

关于java - 如何最好地显示 JGraphX?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3925103/

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