[英]Jung Graph doesn't get displayed on Jpanel
我正在使用NetBeans創建一個應該能夠顯示圖形的SWING應用程序。 在演示應用程序中,我能夠使用以下代碼在JFrame上顯示生成的JUNG圖:
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);
現在,我想使用Palette和設計模式,所以我創建了一個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中。 我沒有收到任何錯誤或警告,並且圖形存在,因為它會為其頂點和邊緣打印一些信息。 有什么想法嗎?
通常在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布局是什么,因此右鍵單擊窗口小部件,然后單擊“設置布局”,然后選擇所需的布局。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.