我正在尝试更改图形流中图形的大小,但它失败了,甚至变小了。
这是我的代码。起初,我无法设置窗口标题,所以我唯一的选择是创建一个 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.addAttribute("ui.stylesheet", "node#D{ fill-color:red; }node{ 描边模式:纯色;大小:20;文本大小:30;描边颜色:#333333;文本背景颜色:#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
}
}
当我运行它时,窗口大小很小,我必须手动调整它的大小。
问题出在哪里?
当我运行它时,窗口大小很小,我必须手动调整它的大小。
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()
方法将根据添加的所有组件的首选大小重置大小。