gpt4 book ai didi

java - Graphstream View 未加载

转载 作者:行者123 更新时间:2023-12-02 03:40:25 28 4
gpt4 key购买 nike

嗯,我有以下架构:

  • 扩展 JFrame 的 Java 类,实例化另一个类包含我的图表,该图表返回空白屏幕。

  • 第二个普通类,其内部有 main 方法,调用包含我的图表的同一类返回正常图表。

现在,为什么 JFrame 类返回空白图?

我的图形类代码。

public class grafoComparacao implements ViewerListener {
private List<Comparados> integralizacoesComparadas;
private Viewer viewer;
private Graph graph;
private View view;
protected boolean loop = true;

public grafoComparacao(List<Comparados> listaComparados) throws HeadlessException, InterruptedException {
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
this.integralizacoesComparadas = listaComparados;
graph = new MultiGraph("clicks");
graph.addAttribute("ui.stylesheet", GraphsStreamStylesheet.stylesheet);

adicionaNodes(graph);

viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
viewer.enableAutoLayout();
view = viewer.addDefaultView(false);

JFrame frame = new JFrame("Comparação de catálogos");
frame.add((Component) view);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

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

frame.setVisible(true);

while(loop) {
fromViewer.pump();
}
}

//this actually works too.
// public static void main(String[] args) throws HeadlessException, InterruptedException{
// Control.Controller ctrl = new Controller();
// JFileChooser escolherAlunos = new JFileChooser();
// escolherAlunos.setMultiSelectionEnabled(true);
// escolherAlunos.showOpenDialog(null);
// File[] integralzacoes = escolherAlunos.getSelectedFiles();
// List<Comparados> integralizacoesComparadas = ctrl.geraComparacaoIntegralizacoes(integralzacoes);
// try {
// grafoComparacao comparacao = new grafoComparacao(integralizacoesComparadas);
// } catch (HeadlessException ex) {
// Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
// } catch (InterruptedException ex) {
// Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
// }
// new grafoComparacao(integralizacoesComparadas);
// }

@Override
public void viewClosed(String string) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void buttonPushed(String string) {

}

@Override
public void buttonReleased(String string) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

private void adicionaNodes(Graph graph) {
//function that populate my graph
}

}

大多数人会说,为什么要在这里创建 JPANEL?我没有找到如何设置它的默认大小,但即使从内部删除 jpanel,它也不起作用,显示一个空白查看器,没有任何异常或其他情况。

这是我调用类(class)做我必须做的事情的地方。

  JFileChooser escolherAlunos = new JFileChooser();
escolherAlunos.setMultiSelectionEnabled(true);
escolherAlunos.showOpenDialog(null);
File[] integralzacoes = escolherAlunos.getSelectedFiles();
List<Comparados> integralizacoesComparadas = ctrl.geraComparacaoIntegralizacoes(integralzacoes);
try {
grafoComparacao comparacao = new grafoComparacao(integralizacoesComparadas); // heres the graph call.
} catch (HeadlessException ex) {
Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
}

创建 SwingWorker 后,图形已呈现,但用户交互消失了。

Swing Worker,这里我只是收集一些信息,让图形类能够处理信息。

public class VisualizacaoWorker extends SwingWorker<Void, Void>{
private List<Comparados> listao;
public VisualizacaoWorker(List<Comparados> lista){
this.listao = lista;
}

@Override
protected Void doInBackground() throws Exception {
GrafoComparacao gf = new GrafoComparacao(this.listao);
return null;
}

}

在这里,它被称为。

JFileChooser escolherAlunos = new JFileChooser();
escolherAlunos.setMultiSelectionEnabled(true);
escolherAlunos.showOpenDialog(null);
File[] integralzacoes = escolherAlunos.getSelectedFiles();
List<Comparados> integralizacoesComparadas = ctrl.geraComparacaoIntegralizacoes(integralzacoes);
VisualizacaoWorker visualizacaoWorker = new VisualizacaoWorker(integralizacoesComparadas);
visualizacaoWorker.execute();

最佳答案

首先,类名应该以大写字符开头。 “grafoComparacao”应为 GrafoComparacao

Now, why the class that is a JFrame returns a blank graph?

我猜测您的 GrafoComparacao 类是在某些监听器代码中创建的,这意味着该代码在负责绘制 GUI 的事件调度线程上执行。

while(loop) 
{
fromViewer.pump();
}

这看起来像是在 EDT 上执行的阻塞代码,意味着您正在使用无限循环,这意味着代码永远不会完成执行,因此 GUI 永远无法响应事件或重新绘制自身。

该代码应该在单独的线程(可能是 SwingWorker)上执行,以便 EDT 可以自由地响应事件并重新绘制自身。

阅读 Swing 教程中关于 Concurrency in Swing 的部分有关 EDT 和 SwingWorker 的更多信息。

关于java - Graphstream View 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36899115/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com