0
...

SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>(){
String a = "a";

    getA() {
         return a;
    }

    protected boolean doInBackground() throws Exception{
        return true;
    }
}

worker.getA();

worker.execute;

...

The code above isn't working, it tells me that getA() (when called in the row before the last) is undefined for SwingWorker.

However, if I make the SwingWorker a separate class, it works without any problems. I may not understand how this works (and I probably don't), but I see no reason making SwingWorker an anonymous inner class would prevent me to call custom methods in it...

Is this even a good idea, or should I make it a separate class? I have no intention of using the SwingWorker anywhere else.

4

1 回答 1

3

This has nothing to do with SwingWorker per se and all to do with your trying to call a method of an anonymous inner class that is not an override of the parent class. Since this cannot be done you simply shouldn't attempt to do this. The solution is to not use an anonymous inner class but rather to use either a private inner class or a stand alone outer class. For example, see the class that I have already posted in your previous question.

于 2013-03-30T14:49:31.747 回答