swing 组件的命名策略

发布于 2024-09-17 10:15:39 字数 241 浏览 8 评论 0原文

在我们的 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 技术交流群。

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

发布评论

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

评论(3

白日梦 2024-09-24 10:15:39

我通常将 JPanel 中的字段存储在 JPanel 的属性中,如下所示:

private JLabel firstNameLabel;
private JTextField firstNameTextField;

在实例化和布置这些组件的例程结束时,您可以运行使用 java.lang.reflect 的例程循环遍历面板的每个属性。如果某个属性源自 Component 类,则可以使用该属性的名称对其调用 setName。例如,它最终会调用:

this.firstNameLabel.setName("firstNameLabel");
this.firstNameTextField.setName("firstNameTextField");

... except through java.lang.reflect

您还可以让例程检查变量的不规则大小写名称,并将其替换为带空格的标准大小写。这将使它们更具可读性。

这种方法将确保无论您添加到面板中的组件是什么,它们都将获得友好的名称。

I typically store the fields in a JPanel in properties on the JPanel, like this:

private JLabel firstNameLabel;
private JTextField firstNameTextField;

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 class Component, you can call setName on it with the name of the property. So for example, it would end up calling:

this.firstNameLabel.setName("firstNameLabel");
this.firstNameTextField.setName("firstNameTextField");

...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.

爱给你人给你 2024-09-24 10:15:39

我通常对所有组件使用复合名称;该名称是基于父名称(例如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 several JInternalFrame 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).

美人迟暮 2024-09-24 10:15:39

你是如何构建界面的? 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.

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