我在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] 删除。
我来说两句