【发布时间】:2015-11-18 21:43:27
【问题描述】:
我正在 NetBeans 中构建一个涉及 GUI 的 Java 应用程序,我试图通过它使用 JGraphX 显示一个图形(表示自动机/有限状态机)。由于某种我无法弄清楚的原因,当我运行程序时,图表没有出现。
我之前在不同的项目中以几乎相同的方式使用过 JGraphX,然后它工作得非常好。
Screenshot showing temp GUI with component (automataBuilder1)
这是代表组件的代码的相关sn-p:
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.swing.handler.mxGraphHandler;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.util.mxMorphing;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;
import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
//Some imports may be unnecessary at the moment.
public class AutomataBuilder extends JPanel {
private final mxGraph graph;
private final mxGraphComponent graphComp;
private final mxGraphHandler graphHand;
private mxGraphModel model;
private Automata automata;
//constructor
public AutomataBuilder(){
graph = new mxGraph();
graphComp = new mxGraphComponent(graph);
graphHand = new mxGraphHandler(graphComp);
automata = new Automata(); //The automata the graph represents
graphComp.setBorder(null);
graphComp.setEnabled(false);
graphComp.setToolTips(true);
this.setLayout(new BorderLayout());
this.add(graphComp);
}
.
. //other methods, not relevant here.
.
public void update(){
Object parent = graph.getDefaultParent();
//Graph already represented on the backend, can get nodes and edges from this
HashMap nodes = getAllNodes(); //Returns all nodes for graph
HashSet edges = getAllEdges(); //Returns all edges for graph
//begin model update
graph.getModel().beginUpdate();
try
{
//iterate through each node and add to graph using insertVertex
Iterator it = nodes.entrySet().iterator();
while(it.hasNext()){
Map.Entry pair = (Map.Entry)it.next();
graph.insertVertex(parent, (String)pair.getKey(), pair.getValue(), 0, 0, 15, 15);
}
//iterate through each edge and add to graph using insertVertex
it = edges.iterator();
while(it.hasNext()){
Edge current = (Edge)it.next();
graph.insertEdge(parent, current.printEdge(), current, current.getFrom(), current.getTo());
}
}
finally
{
graph.getModel().endUpdate();
}
}
}
在 GUI 上按下 Jbutton 时调用更新方法
Screenshot of Application running, graph not there!
我对该程序的测试似乎表明我的逻辑正在正常工作。我可以自己获取单元格并打印出它们的 ID,但图表不会显示!我已经尝试了很多我在网上找到的不同的东西,但总而言之,我的搜索让我更加困惑:S。我意识到这可能是我忽略的一些简单问题,但如果有人可以向我指出问题所在,我将不胜感激!
【问题讨论】:
标签: java swing netbeans jpanel jgraphx