关于 Swing 组件扩展的经验法则是什么?

发布于 2024-11-10 00:11:48 字数 727 浏览 0 评论 0原文

当将一个类专用于特定的 Swing 组件时,扩展该特定组件更好,还是在内部构造它并提供引用更好?

public class Foo extends JComponent{

}

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< /code> 及其所有组件及其各自的侦听器不会暴露我刚刚创建的组件的内部结构。

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(){
    }
}

EDIT

This is what I mean by dedicating

public class Foo{
    private static Foo INSTANCE;

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

沉鱼一梦 2024-11-17 00:11:48

+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

你的背包 2024-11-17 00:11:48

我同意jzd的观点,这取决于情况。

从技术上讲,如果您正在处理 GUI,我认为最好在需要时构建组件,例如通过扩展 JComponent。这样您就可以简单地重复使用它们。

就我个人而言,我永远不会在课堂上使用第二个选项。仅当有充分理由这样做时,我才会让一个类返回另一个组件,例如,使用户能够修改复杂日历组件中的按钮外观。

出于一个非常简单的原因,每个组件类都应该知道它拥有该组件的用途,并且应该根据正在发生的情况来控制视图。这样你就有了合适的方法。

I agree with jzd it all depends.

Technically speaking, if you are dealing with GUI in my opinion it is best to build components when you need them, by extending for example JComponent. This way you can simply reuse them.

Personally I would never use the 2nd option in my class. I would only have a class return another component only if there is a very good reason for doing so, e.g. to enable user to modify a button look in your complex calendar component.

For a very simple reason each component class should know what it has this component for, and it should control the view accordingly to what is happening. Thus you would have appropriate methods.

倾城花音 2024-11-17 00:11:48

我想说延长它会更好。能够使用它的所有属性并像使用该对象一样使用它,使其使用起来更加简单。只是我个人的意见。两种方式都很好。

如果你把整个班级都奉献给它。不妨通过继承来做到这一点。

I would say extending it would be better. Being able to use all its properties and using it like it is that object makes it a lot simpler to use. Just my personal Opinion. Both ways are good.

If you are dedicating the entire class to it. Might as well make it that by inheritence.

网名女生简单气质 2024-11-17 00:11:48

如果你的对象是一个组件,那么扩展它。如果没有,则使用组合。

If your object IS a component, than extend it. If not, then use composition.

篱下浅笙歌 2024-11-17 00:11:48

这实际上取决于你在做什么。例如,如果您想在 JPanel 上包含新类,则需要扩展该组件。如果您的代码可以将组件添加到 GUI 上的正确位置,则无需扩展它。

It really depends on what you are doing. If you want to include your new class on a JPanel for example, you will need to extend the component. If your code can add the component to the correct place on the GUI, then you don't have to extend it.

坏尐絯 2024-11-17 00:11:48

我想说没有一个。 Swing 组件非常(非常)丰富,可以以任何方式定制可视化(L&F)和行为(事件)。另一点是创建一组不同的组件并将它们放置在 JPanel 中。

I would say none of them. Swing components are very (very) rich and can be customized for visualisation (L&F) and behaviour (events) in any manner. Another point is to create a group of different components and lay them out in a JPanel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文