swing 组件的命名策略
在我们的 Swing 应用程序中,我们在 QA (Qf-Test) 中使用自动化测试工具,该工具在命名 Swing 组件时效果更好。 (调用 Component.setName)。尽管它们的自动名称分配工作得相当好,但我们将 SwingX 组件引入到项目中,这给该工具带来了一些问题。
屏幕上有很多潜在的组件(典型的业务应用程序数据输入屏幕,但其中很多 - 该应用程序处于 ERP 的复杂级别),有哪些选项可以以相当不显眼的方式命名 Swing 组件?
In our Swing application, we are using an automated testing tool in QA (Qf-Test) that works better when the swing components are named. (calling Component.setName). Although their automatic name assignments work reasonably well, we are introducing SwingX components into the project and that is causing some issues for the tool.
There are a lot of potential components on a screen (your typical business app data entry screens, but a lot of them - the app is on the complexity level of an ERP), what options are there for naming swing components in a reasonably unobtrusive manner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常将 JPanel 中的字段存储在 JPanel 的属性中,如下所示:
在实例化和布置这些组件的例程结束时,您可以运行使用
java.lang.reflect
的例程循环遍历面板的每个属性。如果某个属性源自Component
类,则可以使用该属性的名称对其调用setName
。例如,它最终会调用:... except through
java.lang.reflect
您还可以让例程检查变量的不规则大小写名称,并将其替换为带空格的标准大小写。这将使它们更具可读性。
这种方法将确保无论您添加到面板中的组件是什么,它们都将获得友好的名称。
I typically store the fields in a JPanel in properties on the JPanel, like this:
At the end of the routine that instantiates and lays out these components, you could run a routine that uses
java.lang.reflect
to loop through each property of the panel. If a property descends from the classComponent
, you can callsetName
on it with the name of the property. So for example, it would end up calling:...except through
java.lang.reflect
You could also have the routine examine the bumpy case names of variables and replace it with standard case with spaces. This would make them more readable.
This approach will ensure that no matter what components you add to a panel, they will all get friendly names.
我通常对所有组件使用复合名称;该名称是基于父名称(例如
JDialog
)和字段名称(通过反射获得)构建的。这使得组件名称在大多数情况下都是唯一的(根据您如何命名父级以及如何使用它们,您仍然可能会发生冲突,例如,如果您打开多个JInternalFrame
并且它们都具有相同的名称...)在 Guts-GUI 中实施了通用命名策略(就是这样做的)。
顺便说一句,组件命名不仅对于 UI 测试很有趣,而且对于资源注入策略(组件的 i18n)也可能很有用。
I generally use a compound name for all my components; the name is built upon the parent name (eg a
JDialog
) and the field name (obtained by reflection). That makes component names unique in most cases (you still may have conflicts depending on how you name parents and how you use them, e.g. if you open severalJInternalFrame
and all have the same name...)I have implemented a generic naming strategy (that does just that) in Guts-GUI.
By the way, component naming is not only interesting for UI testing, but it may also be useful for resource injection strategies (i18n of components).
你是如何构建界面的? JFormDesigner 和可能大多数其他编辑器都允许自动设置组件的名称,这非常方便。大多数时候我根本不需要考虑组件名称。唯一的例外是未使用 JFormDesigner 放置的组件。
How are you building the interface? JFormDesigner and probably most of the other editors allow to set the name of the component automatically, which is very handy. Most of the time I don't have to think about the component names at all. Only exceptions are components that are not placed with JFormDesigner.