How To Structure a Swing Application

前端 未结 2 667

I don\'t really have much experience with swing, or GUI design in general for that matter (a few WPF applications in university is about the height of it), however I have been t

2条回答
  •  梦如初夏
    2021-02-04 15:45

    What if you were to set up an abstract class that creates the 50% shared code, and then extend from that?

    For example:

    abstract class BasePopupPanel extends JPanel {
    
        public void initialize() {
            // Initialize all the shared code here.
            // eg. add(new JButton("TEST");
        }
    }
    

    And now you create the actual popup panels:

    public class GiraffePopupPanel extends BasePopupPanel {
    
        public void initialize() {
            super.initialize();
            // Here you do all the initializations for this class.
        }
    }
    

    You can create as many of these as you would like. When it comes time to add them...

    ...let's say you have a method that is called displayPopup, then the signature would look like this:

    public void displayPopup(BasePopupPanel popup) {
        // do stuff regarding JDialogs, etc.
        // ...
        popup.initialize();
        // do more stuff...
    }
    

    I hope that gives you one view on how you could refactor your classes.

提交回复
热议问题