Create a Swing component based on another but with different API

前端 未结 4 1502
礼貌的吻别
礼貌的吻别 2021-01-24 10:36

I would like to create a new Swing JComponent based on an existing one, but with a different API. In other words, I don\'t want to extend the existing component, because I don\'

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-24 11:12

    You can create MyJComponent subclass of JComponent with a private field that references a forwarding class for ExistingComponent.
    The interactions with ExistingComponent are done with the forwarding class through methods of MyJComponent, and you are free to add more methods to MyJComponent.
    Please see Effective Java item 16, for the delegation pattern used with the forwarding class.

提交回复
热议问题