Graphstream在jframe内部很小

时间:2016-04-23 15:05:45

标签: java swing graphstream

我正在尝试更改图形流内部图形的大小,但它的失败甚至变小。

这是我的代码。起初,我无法设置窗口标题,所以我唯一的选择是创建一个JFRAMe并将视图嵌入其中。所以,如果有一个选项来设置图表的标题,我将不胜感激。

这是我的代码。

public class Clicks implements ViewerListener {
    protected boolean loop = true;

    public static void main(String args[]) throws InterruptedException {
        new Clicks();
    }
    public Clicks() throws InterruptedException {
             System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
        // We do as usual to display a graph. This
        // connect the graph outputs to the viewer.
        // The viewer is a sink of the graph.
        Graph graph = new MultiGraph("Clicks");
                graph.addAttribute("ui.stylesheet", "node#D{ fill-color:red; }node{ stroke-mode: plain;size:20;text-size:30;stroke-color:#333333;text-background-color:#FFF;text-background-mode:plain;text-alignment:above;}");




                Node D = graph.addNode("D");
                Node J = graph.addNode("J");
        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
                View view = viewer.addDefaultView(false);


                JFrame frame = new JFrame("title");
                frame.setSize(1000, 1000);
                frame.add((Component) view);
                frame.pack();
                frame.setVisible(true);

                ViewerPipe fromViewer = viewer.newViewerPipe();
        fromViewer.addViewerListener(this);
        fromViewer.addSink(graph);



        while(loop) {
            fromViewer.blockingPump();// or fromViewer.blockingPump(); in the nightly builds
        }
    }

    public void viewClosed(String id) {
        loop = false;
    }

    public void buttonPushed(String id) {

    }

    public void buttonReleased(String id) {
        System.out.println("Button released on node "+id);
    }
}

@edit

即使应用所提出的更改,它也无效。

public Clicks()抛出InterruptedException {          System.setProperty(“org.graphstream.ui.renderer”,“org.graphstream.ui.j2dviewer.J2DGraphRenderer”);     //我们照常显示图表。这个     //将图形输出连接到查看器。     //查看器是图形的接收器。     Graph graph = new MultiGraph(“Clicks”);             graph.addAttribute(“ui.stylesheet”,“node #D {fill-color:red;} node {stroke-mode:plain; size:20; text-size:30; stroke-color:#333333; text-background - 颜色:#FFF;文本背景模式:平纹;文本对齐:上面;}“);

        Node D = graph.addNode("D");
        Node J = graph.addNode("J");
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
        View view = viewer.addDefaultView(false);


        JFrame frame = new JFrame("title");
        frame.add((Component) view);


        ViewerPipe fromViewer = viewer.newViewerPipe();
fromViewer.addViewerListener(this);
fromViewer.addSink(graph);

        frame.pack();
        frame.setVisible(true);


while(loop) {
    fromViewer.blockingPump();// or fromViewer.blockingPump(); in the nightly builds
}

}

当我运行它时,窗口大小很小,我必须手动调整它的大小。

问题是什么?

1 个答案:

答案 0 :(得分:1)

  

当我运行它时,窗口大小很小,我必须手动调整它的大小。

frame.pack();
frame.setVisible(true);

ViewerPipe fromViewer = viewer.newViewerPipe();
fromViewer.addViewerListener(this);
fromViewer.addSink(graph);

在使框架可见之前,您需要将所有组件添加到框架中。

所以代码应该是:

ViewerPipe fromViewer = viewer.newViewerPipe();
fromViewer.addViewerListener(this);
fromViewer.addSink(graph);

frame.pack();
frame.setVisible(true);

此外,不需要以下一行:

//frame.setSize(1000, 1000);

pack()方法将根据添加的所有组件的首选大小重置大小。