Java Swing组件的名称约定(前缀) [英] Name convention on Java Swing components(prefix)

查看:154
本文介绍了Java Swing组件的名称约定(前缀)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中使用Swing进行编程时,我想到的一个问题是,它是Swing组件上的推荐还是官方/最常用的命名约定(前缀)。

An question that was brought to my mind when programming with Swing in Java was, is it a recommendation or an "official"/most used naming convention(prefix) on Swing components.

例如(即使其他人可能更喜欢其他命名约定,这就是我目前使用的):

Such as(even though other may prefer other naming conventions this is what I am currently using):


  • txt for JTextField

  • btn for JButton

  • lbl for JLabel

  • pnl对于JPanel

  • txt for JTextField
  • btn for JButton
  • lbl for JLabel
  • pnl for JPanel

然后我的列表结束..

But then my list ends..

我认为这样前缀增强了我的代码中的可读性,但我没有任何组件的名称,如JComboBox,JList,JRadioButton,JCheckButton等等,列表继续。

I think such prefixes enhance the readability in my code so, but I do not have any names for components such as JComboBox, JList, JRadioButton, JCheckButton and so the list goes on.

谢谢事先。

推荐答案

人们会告诉你使用前缀是不好的,因为这是匈牙利表示法,现在,匈牙利表示法被认为是编程中的一个大禁忌。就个人而言,作为一个已完成一定数量GUI工作的人,我可以告诉你,在非GUI编程中绝对应该避免匈牙利符号,在做GUI时这是一个很好的做法。


例如,考虑一个带有文本框的简单表单,用于输入用户名。在此文本框前面应该是一个标签,提示用户在文本框中输入他的名字。现在,您如何命名文本框? '名称'?标签怎么样?
一个好的做法应该是在文本框前加上txt,并用label标记,这就是匈牙利符号的全部内容。因此,文本框现在名为txtName,相应的标签名为lblName。这为您提供了在IDE编辑器中轻松访问文本框,组合框和其他小部件的额外好处。例如,在Eclipse中键入txt并按下CTRL + Space会打开一个列出所有文本框的上下文菜单,如果您遵循此表示法。


现在,回答您的问题。定义用于前缀的三个字母的常用方法是从小部件名称中删除所有元音以及所有重复辅音。如果剩下三个辅音,则应忽略它们。因此,文本框(或TextField,或在您首选的小部件工具箱中调用此小部件)变为'txt',标签'lbl',组合框'cmb',表'tbl'等等。

People will tell you that using prefixes is bad, because that is Hungarian notation and nowadays, Hungarian notation is considered a big no-no in programming. Personally, as somebody who has done a certain amount of GUI work, I can tell you that, whereas Hungarian notation should definitely be avoided in non-GUI programming, when doing GUIs it's a very good practice.

Consider, for example, a simple form with a textbox which is to be used to enter the user's name. In front of this textbox should be a label prompting the user to enter his name in the textbox. Now, how are you going to name the textbox? 'Name'? What about the label? A good practice should be prefixing the textbox with txt, and label with label, which is what Hungarian notation is all about. Thus, the textbox is now named 'txtName' and the corresponding label is named 'lblName'. This gives you the additional benefit of easily accessing your textboxes, combo boxes and other widgets when in your IDE's editor. For example, typing 'txt' and pressing CTRL+Space in Eclipse opens up a context menu listing all of your textboxes, if you follow this notation.

Now, to answer your question. The usual way to define what three letters you should use for a prefix is to remove all the vowels from the widget's name and also all repeating consonants. If there are more then three consonants left, they should be ignored. Therefore, a textbox (or TextField, or whatever this widget is called in your preferred widget toolkit) becomes 'txt', a label 'lbl', a combo box 'cmb', a table 'tbl' and so on.

这篇关于Java Swing组件的名称约定(前缀)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆