我正在NetBeans中构建一个涉及GUI的Java应用程序,我试图通过它使用JGraphX显示图形(表示自动机/有限状态机)。由于某种原因,我无法弄清楚,当我运行程序时,图表不会出现。
之前我以一种几乎完全相同的方式使用JGraphX进行不同的项目,然后它的工作非常精细。
Screenshot showing temp GUI with component (automataBuilder1)
以下是代表组件的相关代码片段:
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时调用update方法
Screenshot of Application running, graph not there!
我对程序的测试似乎表明我的逻辑工作正常。我可以自己获取单元格并打印出它们的ID,图表就不会显示了!我已经尝试了很多我在网上找到的不同的东西,但总而言之,我的搜索让我更加困惑:S。我意识到这可能是我忽略的简单事情,但如果有人可以向我指出问题是什么我会很感激!
答案 0 :(得分:0)
好吧,在搞砸了一下后,我意识到我只需将自动生成器容器放在另一个JPanel中。不知道为什么会让它发挥作用,但一切都正常运作。