我正在尝试创建一个JFrame,其中顶部是JGraph的JPanel,底部是条形图的JPanel,它允许用户选择将在图形中出现多少个节点,以及在发生这种情况后要更新的图形。出于这个原因,我将Graph放在一个单独的类中,稍后我将在其中构建一个update方法。
我的问题是,包含Graph的面板是框架的大小,但我的Graph仅与最大顶点值一样大。如何将JPanel更改为特定大小,并让图形填充此空间?
同样,我这样做是实现这个目标的最好方法,还是有更好的方法?
public class MyGraph extends JFrame{
private static final long serialVersionUID = 1L;
private JPanel Gpanel;
public MyGraph(){
super("Graph");
JPanel Gpanel = new NewPanel();
getContentPane().add(Gpanel);
}
public static void main(String args[]){
MyGraph frame = new MyGraph();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class NewPanel extends JPanel{
private static final long serialVersionUID = 1L;
mxGraph graph;
Object parent;
NewPanel(){
this.graph = new mxGraph();
this.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!", 300, 150, 80, 30);
graph.insertEdge(parent, null, "Edge", v1, v2);
}
finally{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
this.add(graphComponent);
}
public void Update(){
}
}}
发布于 2013-11-24 22:35:21
始终设置面板的布局
public MyGraph(){
...
getContentPane().setLayout(new BorderLayout());
getContentPane().add(Gpanel, BorderLayout.CENTER);
}
class NewPanel extends JPanel{
...
NewPanel(){
...
this.setLayout(new BorderLayout());
this.add(graphComponent, BorderLayout.CENTER);
}
}https://stackoverflow.com/questions/20160142
复制相似问题