我正在研究 GraphStream 库。现在,当我运行我的程序时,它会为我的图表打开新窗口并为我的图表打开单独的窗口。我试图创建一个JFrame
并添加JPanel
到JFrame
,毕竟我试图将图形添加到我的JPanel
但它说图形对象不是一个组件。
这是我的代码:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;
public class GraphExplore {
static Connection conn2;
static String result, result2;
static JFrame frame;
static JPanel panel;
public static void main(String args[]) throws SQLException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
showData();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
private static void showData() throws SQLException {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(30, 50, 1300, 600);
panel = new JPanel();
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
panel.setLayout(new BorderLayout(0, 0));
frame.setContentPane(panel);
Graph graph = new SingleGraph("tutorial 1");
graph.setAutoCreate(true);
graph.setStrict(false);
graph.display();
// panel.add(graph);
try {
Class.forName("org.h2.Driver");
conn2 = DriverManager.getConnection("jdbc:h2:file:G:/hs_data/h2_db/test", "sa", "sa");
} catch (Exception e) {
e.printStackTrace();
}
Statement stmt2 = conn2.createStatement();
ResultSet rs2 = stmt2.executeQuery("SELECT ANUMBER,BNUMBER,DATETIME FROM TEST");
while (rs2.next()) {
result = rs2.getString("ANUMBER");
result2 = rs2.getString("BNUMBER");
graph.addNode(result);
graph.addNode(result2);
int i;
for (i = 0; i < 200; i++)
graph.addEdge("string" + i, result, result2);
// JOptionPane.showMessageDialog(null, i);
}
for (Node node : graph) {
node.addAttribute("ui.label", node.getId());
}
}
}
该程序为两者jframe
和图形打开单独的窗口。我想将我的图表显示到我的JFrame
orJPanel
中。关于如何做到这一点的任何想法?我看过这个链接,但它并没有很好地解释我。