GUI不会在每次单击时更新图像,因为图形的新图像在生成每个新图像时都需要花费一些时间,因此GUI会显示前一个图像

萨达夫·尤纳斯(Sadaf Younas)

我在GUI上显示当前内容时遇到问题,我在使用Swing Worker,但它未根据我的要求工作。友善的人帮助我在每次单击时更新图像,但是图像生成需要一定时间,因为图像是通过图表viz生成的。我对我的项目感到震惊...enter code here

private static void show1(){
            SwingWorker<Void,Void> worker1= new SwingWorker<Void,Void>(){
                @Override
                protected Void doInBackground() throws Exception {
                    Thread.sleep(100);
                gp.GraphPanel();// here in graph panel image is not updated         
                    return null;
            }
            protected void done() {
            }
        };      
        worker1.execute();      
            }
    // show1 is called inside action listener 
        public static JScrollPane GraphPanel() 
        {             // some code here
                   ImageIcon imgIcon=new ImageIcon(FILE_NAME.filename+".png");
            label.setIcon( imgIcon);
            pane2.add(label);
                    JScrollPane grphPane= new JScrollPane(pane2);
                    return grphPane;

        }
疯狂程序员

Aqua的答案是应该使用的正确方法SwingWorker,但是,对于JScrollPane在该GraphPane方法中创建的实例所得到的结果,您什么也没做

此方法创建GraphPane的新实例。

public static JScrollPane GraphPanel() 
{             // some code here
    ImageIcon imgIcon=new ImageIcon(FILE_NAME.filename+".png");
    label.setIcon( imgIcon);
    pane2.add(label);
    JScrollPane grphPane= new JScrollPane(pane2);
    return grphPane;
}

但是这样称呼...

gp.GraphPanel();

不执行任何操作,您应该将此方法的结果添加到您的UI中...

根据您的代码示例,label似乎是一个实例变量,如果它已经出现在屏幕上,则只需设置其icon属性,然后让UI自行对其进行更新

public void updateGraph() 
{             // some code here
    ImageIcon imgIcon=new ImageIcon(FILE_NAME.filename+".png");
    label.setIcon( imgIcon);
}

另外,请static尽可能避免,这很好地表明您的设计需要工作

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档