【问题标题】:Add GraphStream graph into my custom jPanel将 GraphStream 图添加到我的自定义 jPanel
【发布时间】:2017-12-16 11:12:23
【问题描述】:

我正在研究 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 和图形打开单独的窗口。我想在我的JFrameJPanel 中显示我的图表。关于如何做到这一点的任何想法?我看过这个link,但它并没有很好地解释我。

【问题讨论】:

    标签: java swing graph jpanel graphstream


    【解决方案1】:

    Graph Visualization: Advanced view: Integrating the viewer in your GUI 所示,“您需要自己创建查看器。”另外,在您构建框架之后调用setVisible()

    它在frame.add(view) 上显示错误。

    看起来引用的教程有点过时了。 Viewer 方法 addDefaultView() 现在返回 ViewPanel可以添加到 Container。在下面的完整示例中,在具有GridLayout 的封闭JPanel 上设置了边框,并将panel 添加到框架中。另请注意,需要通过覆盖getPreferredSize()panel 提供首选大小。调整窗口大小以查看效果。

    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);
        }
    }
    

    【讨论】:

    • frame.add(view); 显示错误“类型容器中的方法 add(comp) 不适用于参数(视图)”
    • 我已经在上面详细说明了。
    • 这非常有效。唯一的问题是,所有节点都聚集在一个点上,我必须自己拖动每个节点才能有一个清晰的视野。
    • 如果您没有明确设置节点的"xy" 属性,请尝试viewer.enableAutoLayout()
    猜你喜欢
    • 2022-01-02
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 2017-09-07
    相关资源
    最近更新 更多