将GraphStream图添加到我的自定义jPanel中 [英] Add GraphStream graph into my custom jPanel
问题描述
我正在研究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;
静态字符串结果,result2;
静态JFrame框架;
静态JPanel面板;
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(新的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);
尝试{
Class.forName(org.h2.Driver);
conn2 = DriverManager.getConnection(jdbc:h2:file:G:/ hs_data / h2_db / test,sa,sa);
} catch(Exception e){
e.printStackTrace();
}
语句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); (节点节点:图){
node.addAttribute(ui.label,node.getId());
}
}
}
}
<这个程序为 jframe
和图形打开单独的窗口。我想将我的图表显示到 JFrame
或 JPanel
中。任何想法如何做到这一点?我已经看到这个
import java.awt。*;
import javax.swing。*;
import javax.swing.border。*;
import org.graphstream.graph。*;
import org.graphstream.graph.implementations。*;
import org.graphstream.ui.swingViewer。*;
import org.graphstream.ui.view。*;
/ ** @see https://stackoverflow.com/a/45055683/230513 * /
public class GraphSwing {
public static void main(String args []){
EventQueue.invokeLater(new GraphSwing():: display);
}
private void display(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout()){
@Override
public Dimension getPreferredSize(){
return new Dimension(640,480);
}
};
panel.setBorder(BorderFactory.createLineBorder(Color.blue,5));
Graph graph = new SingleGraph(Tutorial,false,true);
graph.addEdge(AB,A,B);
节点a = graph.getNode(A);
a.setAttribute(xy,1,1);
节点b = graph.getNode(B);
b.setAttribute(xy,-1,-1);
Viewer viewer = new Viewer(graph,Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
ViewPanel viewPanel = viewer.addDefaultView(false);
panel.add(viewPanel);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
I am working on GraphStream library. For now, When I run my program it opens new window for my graph and separate window for my graph. I tried to create a JFrame
and add JPanel
into JFrame
, after all this I tried to add graph into my JPanel
but it says that graph object is not a component.
Here is my code:
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());
}
}
}
This program opens separate windows for both jframe
and graph. I want to show my graph into my JFrame
or JPanel
. Any idea about how to do this? I have seen this link, but it doesn't explains me well.
As shown in Graph Visualization: Advanced view: Integrating the viewer in your GUI, "you will need to create the viewer by yourself." Also, call setVisible()
after you have constructed the frame.
It shows error on
frame.add(view)
.
It looks like the tutorial cited is a little dated. The Viewer
method addDefaultView()
now returns a ViewPanel
, which can be added to a Container
. In the complete example below, a border is set on an enclosing JPanel
having GridLayout
, and that panel
is added to the frame. Also note the need to give the panel
a preferred size by overriding getPreferredSize()
. Resize the window to see the effect.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;
import org.graphstream.ui.swingViewer.*;
import org.graphstream.ui.view.*;
/** @see https://stackoverflow.com/a/45055683/230513 */
public class GraphSwing {
public static void main(String args[]) {
EventQueue.invokeLater(new GraphSwing()::display);
}
private void display() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout()){
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
panel.setBorder(BorderFactory.createLineBorder(Color.blue, 5));
Graph graph = new SingleGraph("Tutorial", false, true);
graph.addEdge("AB", "A", "B");
Node a = graph.getNode("A");
a.setAttribute("xy", 1, 1);
Node b = graph.getNode("B");
b.setAttribute("xy", -1, -1);
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
ViewPanel viewPanel = viewer.addDefaultView(false);
panel.add(viewPanel);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
这篇关于将GraphStream图添加到我的自定义jPanel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!