无法在swing工作者中调用方法

时间:2012-12-13 01:08:25

标签: swingworker

我有一个带ActionListener的按钮并尝试在SwingWorker中调用CopyTask方法但是eclipse说错误     “对于ActionListener(){}”类型,未定义CopyTask(File,File)方法。你能帮帮忙吗

//imported everything needed
 public class A extends JFrame implements PropertyChangeListener
 {
 File src;
 File dest;
 CopyTask task;
//other components
          JTextFiles jt = new JTextField();


                                 src = new File(jt.getText()); //getting input from JTextField
                     dest = new File ("\\C$\\Web"); //providing the location manually

                CopyTask task = this.new CopyTask(src, dest);
                task.addPropertyChangeListener(this);
                        task.execute();

             @Override
       public void propertyChange(PropertyChangeEvent evt)
       {
           if("progress".equals(evt.getPropertyName()))
           {
               int progress = (Integer) evt.getNewValue();
               progressAll.setValue(progress);
           }
       }



       class CopyTask extends SwingWorker<Void, Integer>
       {
           private File source;
           private File target;
           private long totalBytes = 0;
           private long copiedBytes = 0;

           public CopyTask(File src, File dest)
           {
               this.source = src;
               this.target = dest;

               progressAll.setValue(0);
          //     progressCurrent.setValue(0);
           } 

   @Override
   public Void doInBackground() throws Exception
           {
               ta.append("Retrieving some info ... ");
               retrieveTotalBytes(source);
               ta.append("Done!\n");

               copyFiles(source, target);
               return null;
           }

1 个答案:

答案 0 :(得分:0)

您是否可以稍微重构您的代码,以便它可以完全独立地用作示例。我现在无法真正看到(从您的代码示例中)按钮发挥作用的位置?

示例:

public class A extends JFrame {

  File src;
  File dst;
  JButton button;

  public A() {
    // Add button to JFrame

    // Set button action
    button.setAction(new AbstractAction() {
      public void actionPerformed() {
        CopyTask ct = new CopyTask(src, dst);
        ct.execute();
      }
    }
  }

  class CopyTask extends SwingWorker<Void, Integer> { ... }
}