英文:
How to divide up the constructor in Java Swing?
问题
我瞥了一眼来自NetBeans项目的Java Swing类代码,它在其构造函数中有一个initComponents()
方法,以及一个initializeData()
方法。
initComponents()
方法是自动生成的,然而对我来说,将构造函数代码分为组件初始化和组件数据初始化代码似乎是一个很好的实践。
在组件初始化代码中,您会初始化布局、文本字段的可编辑属性、背景颜色等。
在组件数据初始化代码中,您会初始化文本属性等。
这个想法是否可以推广到非NetBeans项目的更或多或少普遍的规则?(我使用Eclipse和Window Builder插件)。如何最好地划分构造函数代码?
英文:
I've glanced at a Java Swing class' code from a NetBeans project and it has an initComponents()
method in its constructor, as well as an initializeData()
method.
The initComponents()
method is auto-generated, however it seems a good practice to me to divide your constructor code between component initialization and component data initialization code.
In the component init code you would initialize layouts, the editable property of text fields, background colors, etc.
In the component data init code you would initialize the text properties, etc.
Can this be extrapolated to a more-or-less general rule for non-NetBeans projects? (I work with Eclipse and the Window Builder plug-in). What would be the best way to divide up that constructor code?
专注分享java语言的经验与见解,让所有开发者获益!
评论