0

Swing Worker を拡張するクラスに多くの引数 (正確には 10 個) を渡しているので、コードをよりエレガントで読みやすくする補助クラスを作成する方法があるかどうか疑問に思っていました。task オブジェクトはクラスです。コードは適切に機能していますが、渡す引数をさらに追加することにした場合、コードをより読みやすく最適化できるかどうか疑問に思っていました。

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    SortThread task = new SortThread(a, brelem, sort, jButton1, brElemTextField,
            bubbleButton, selectionButton, insertionButton, jLabel3,
            konzola, startTime, stopTime, sekunde, jScrollPane2);
    task.addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent e) {
            if ("progress".equals(e.getPropertyName())) {
                jProgressBar1.setValue((Integer) e.getNewValue());
            }
        }
    });
    task.execute();

}        
4

2 に答える 2

4

I can't really provide a good answer for this question, as I don't have much idea what it is you're trying to do. I strongly recommend looking up reading material on refactoring and design patterns, as experience with such techniques will help you always and forever.

As to the specific example: It seems like most of the arguments are UI controls - you could try making an object which contains your user interface objects, which you can then pass into the constructor in place of all of the objects. EDIT: You don't necessarily need to make a class of your own for this - a map is also a possibility.

Alternatively, if you make SortThread an inner class, it can then have access to the fields of the parent, so you don't have to pass them in.

Of course, others with more experience will likely have more to say on the matter, but given that this question is so broad, I thought I'd throw in a couple of options anyway.

于 2012-12-05T22:32:50.663 に答える
2

非常に多くの GUI コンポーネントをワーカーに渡す代わりに、ビューを としてワーカーに登録しPropertyChangeListenerます。SwingWorkerには、オブザーバー パターンを非常に簡単firePropertyChange()に使用できるメソッドがあります。

于 2012-12-06T13:10:41.820 に答える