0

我有一个带有 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;
           }
4

1 回答 1

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> { ... }
}
于 2012-12-25T10:10:52.477 回答