2014-01-14 85 views
2

我正在使用NetBeans创建一个应该能够显示图形的SWING应用程序。在演示应用程序,我能够用下面的代码在JFrame显示生成JUNG图:Jung图不会显示在Jpanel上

UndirectedGraph t = GraphML.CreateGraph("treeAttribute.graphml"); 
VisualizationViewer<Node, Edge> vv = new VisualizationViewer<Node, Edge>((new FRLayout<Node, Edge>(t))); 
vv.getRenderContext().setVertexFillPaintTransformer(new VertexPainter()); 
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>()); 
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Edge>()); 

final JFrame frame = new JFrame(); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.getContentPane().add(vv); 
frame.pack(); 
frame.setVisible(true); 

现在我想利用调色板和设计模式,所以我创建了一个JFrame,我有插入一个JPanel。我希望我的图形显示在JPanel内部,因此我已将图形的代码插入到initComponents()方法中。代码如下所示:

public class Main extends javax.swing.JFrame { 


public Main() throws ParserConfigurationException, SAXException, IOException { 
    initComponents(); 

} 

@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    jPanel1 = new javax.swing.JPanel(); 
    jLabel1 = new javax.swing.JLabel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 634, Short.MAX_VALUE) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 298, Short.MAX_VALUE) 
    ); 

    jLabel1.setText("Graph A"); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jLabel1) 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addContainerGap(45, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(39, 39, 39) 
      .addComponent(jLabel1) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(42, Short.MAX_VALUE)) 
    ); 

    try { 

     UndirectedGraph t = GraphML.CreateGraph("treeAttribute.graphml"); 
     VisualizationViewer<Node, Edge> vv = new VisualizationViewer<Node, Edge>((new FRLayout<Node, Edge>(t))); 
     vv.getRenderContext().setVertexFillPaintTransformer(new VertexPainter()); 
     vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>()); 
     vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Edge>()); 
     jPanel1.add(vv); 

    } catch (ParserConfigurationException ex) { 
     Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (SAXException ex) { 
     Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IOException ex) { 
     Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
    } 

    pack(); 
}// </editor-fold>       

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 

    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       new Main().setVisible(true); 


      } catch (ParserConfigurationException ex) { 
       Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (SAXException ex) { 
       Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (IOException ex) { 
       Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
      } 

     } 
    }); 

} 

// Variables declaration - do not modify      
private javax.swing.JLabel jLabel1; 
private javax.swing.JPanel jPanel1; 
// End of variables declaration     
} 

该程序正常运行,但没有任何显示到JPanel中。我没有收到任何错误或警告,图表就在那里,因为它打印了一些顶点和边缘的信息。有什么想法吗?

回答

0

通常在Netbeans的,这足以显示图形:

// say your graph is called g 
layout = new FRLayout<>(g); 
((FRLayout<yournode, youredge>)layout).setMaxIterations(1000); 
layout.setSize(new Dimension(700, 700)); 
VisualizationViewer<yournode, youredge> vv = new VisualizationViewer<>(layout); 
GraphZoomScrollPane scrollPane = new GraphZoomScrollPane(vv); 
JPanel1.add(scrollPane); 

,但你应该知道,你要告诉的Netbeans什么是JPanel的布局,让您的小工具单击鼠标右键,然后单击“设置布局“并选择你喜欢的。