如何使用jung添加菜单来更改图形的布局

时间:2011-01-26 10:49:41

标签: java jung

如何添加q菜单栏来更改Jung图的布局(即:StaticLayout,SpringLayout等)?


事实上,这就是我已经拥有的:

JFrame frame = new JFrame("JUNG2 based GraphVisualization Tool");
        // Create a graph 
        SparseMultigraph<MyNode, MyEdge> graph = new SparseMultigraph<MyNode, MyEdge>();
        // We want to give the Nodes a point where to be (for later use)
        //Map<MyNode, Point2D> vertexLocations = new HashMap<MyNode, Point2D>();
        // Also we need a Layout
        Layout<MyNode, MyEdge> layout = new StaticLayout(graph);
        layout.setSize(new Dimension(600, 600));
        // VisualizationViewer to Visualize our nodes and edges
        VisualizationViewer<MyNode, MyEdge> vv = new VisualizationViewer<MyNode, MyEdge>(layout);
        vv.setPreferredSize(new Dimension(650, 650));
        // To show the vertex and EdgeLabels
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
        // Our mouse should be usable in different modes
        EditingModalGraphMouse mouse = new EditingModalGraphMouse(vv.getRenderContext(), MyNodeFactory.getInstance(), MyEdgeFactory.getInstance());
        // Default values for new edges
        MyEdgeFactory.setDefaultCapacity(100.0);
        MyEdgeFactory.setDefaultWeight(5.0);

        // Popupmenu
        PopupNodeEdgeMenuMousePlugin nodeEdgePlugin = new PopupNodeEdgeMenuMousePlugin();
        JPopupMenu nodeMenu = new MyMouseMenus.NodeMenu();
        JPopupMenu edgeMenu = new MyMouseMenus.EdgeMenu(frame);
        nodeEdgePlugin.setNodePopup(nodeMenu);
        nodeEdgePlugin.setEdgePopup(edgeMenu);
        // The already existing popup editing plugin has to be removed
        mouse.remove(mouse.getPopupEditingPlugin());
        // And the new one has to be added
        mouse.add(nodeEdgePlugin);
        // set up the new mouse for the VisualizationViewer
        vv.setGraphMouse(mouse);

        // A JFrame to show all the stuff
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(vv);

        // To change the mouse modes, the tutorial shows a menuBar. Think it would be nice to have a toolbar here!
        JMenuBar menuBar = new JMenuBar();
        JMenu modeMenu = mouse.getModeMenu();
        modeMenu.setText("Mouse Mode");
        modeMenu.setIcon(null);
        modeMenu.setPreferredSize(new Dimension(80,20));
        menuBar.add(modeMenu);
        frame.setJMenuBar(menuBar);
        mouse.setMode(ModalGraphMouse.Mode.EDITING);
        frame.pack();
        frame.setVisible(true);

抱歉,我是java的新手,所以如果你根据我的代码建议我该怎么做,那将会很棒。谢谢

1 个答案:

答案 0 :(得分:0)

你有一个Jung图的事实并没有真正改变你需要做的事情。  1.创建一个JMenuBar并将其附加到您需要菜单的JFrame。  2.添加与其绑定的Actions或ActionListeners的JMenuItem。  3.在GUI中创建一个或多个方法,用Graph更改容器的布局。 (可能必须添加/删除组件,或者可能只是完全重建GUI的那一部分)。  4.让ActionListener调用适当的方法。