关于Swing组件扩展的经验法则是什么? [英] What's the rule of thumb regarding Swing component extension?

查看:93
本文介绍了关于Swing组件扩展的经验法则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将类专用于特定的Swing组件时,最好是extend该特定组件,或者在内部构造它并提供引用?

When dedicating a class to a particular Swing component, is it better to extend that particular component, or construct it internally and provide a reference?

public class Foo extends JComponent{

}

OR

public class Foo{
    public JComponent getComponent(){
    }
}

编辑

这就是我所说的专用

public class Foo{
    private static Foo INSTANCE;

    public static Foo getInstance(){
        if(INSTANCE == null){
            INSTANCE = new Foo();
        }
    }

    public void createAndShowComponent(){
        //do stuff
    }
}

createAndShowComponent()内,我创建一个JComponent,其中包含所有组件以及它们各自的侦听器,而没有公开我刚刚创建的组件的内部.

Inside createAndShowComponent(), I create a JComponent with all its components and their respective listeners without exposing the internals of the component I just created.

推荐答案

+1表示合成扩展.因为您只公开了对新组件重要的方法,所以它使API更加整洁

+1 for Composition over extension. It makes the API much cleaner since you only expose what methods are important for your new component

这篇关于关于Swing组件扩展的经验法则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆