关于 Swing 组件扩展的经验法则是什么?
当将一个类专用于特定的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
+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
我同意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.
我想说延长它会更好。能够使用它的所有属性并像使用该对象一样使用它,使其使用起来更加简单。只是我个人的意见。两种方式都很好。
如果你把整个班级都奉献给它。不妨通过继承来做到这一点。
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.
如果你的对象是一个组件,那么扩展它。如果没有,则使用组合。
If your object IS a component, than extend it. If not, then use composition.
这实际上取决于你在做什么。例如,如果您想在 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.
我想说没有一个。 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.