我想在GUI中的JScrollPane中创建简单的图形。我试图通过使用随JGraphX分发的“Hello world”示例来实现,resutlt如下所示:
public class GraphTest{
public GraphTest(FrameWithScrollPane frame)
{
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);
frame.setNewPane(new JScrollPane(graphComponent));
}
public static void main(String[] args)
{
FrameWithScrollPane frame = new FrameWithScrollPane();
frame.setVisible(true);
GraphTest test = new GraphTest(frame);
}
}
在这里,我试图用一个新的JScrollPane替换已经存在的JScrollPane,但我无法让它工作。在JScrollPane中插入图形的唯一方法似乎是拥有一个干净的JFrame,然后创建一个全新的JScrollPane,使用graphComponent作为构造函数的参数...这不是我需要的,已经使用模拟的复杂GUI Netbeans GUI Builder。将graphComponent添加到现有JScrollPane也不起作用。 有没有人有任何想法?