Java Swing 组件的命名约定(前缀)

发布于 2024-10-13 09:39:05 字数 358 浏览 3 评论 0原文

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

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

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

但是我的列表结束了..

我认为这样的前缀增强了我的代码的可读性,所以,但我没有任何组件名称,例如 JComboBox、JList、JRadioButton、JCheckButton 等等。

提前致谢。

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 for JPanel

But then my list ends..

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.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

凡间太子 2024-10-20 09:39:05

人们会告诉您使用前缀是不好的,因为那是匈牙利表示法,而如今,匈牙利表示法被认为是编程中的一大禁忌。就我个人而言,作为做过一定量 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.

尽揽少女心 2024-10-20 09:39:05
  • btn - JButton
  • chk - JCheckBox
  • clr - JColorChooser
  • cmb - JComboBox
  • ico - JDesktopIcon
  • edt - JEditorPane
  • fch - JFileChooser
  • ifr - JInternalFrame
  • lbl - JLabel
  • lyp - JLayeredPane
  • lst - JList
  • mnu - JMenuBar
  • mni - JMenuItem
  • opt - JOptionPane
  • pnl - JPanel
  • pmn - JPopupMenu
  • 程序- JProgressBar
  • rad - JRadioButton
  • rot - JRootPane
  • scb - JScollBar
  • scr - JScrollPane
  • spr - JSeparator
  • sld - JSlider
  • spn - JSpinner
  • spl - JSplitPane
  • tab - JTabbedPaneJTable
  • tbl - JTable
  • tbh - JTableHeader
  • txa - JTextArea
  • txt - JTextField
  • txp - JTextPane
  • tgl - J ToggleButton
  • tlb - JToolBar
  • tlt - JToolTip
  • tre - JTree
  • vpr - JViewport

源 -: http://zuskin.com/java_naming.htm

  • btn - JButton
  • chk - JCheckBox
  • clr - JColorChooser
  • cmb - JComboBox
  • ico - JDesktopIcon
  • edt - JEditorPane
  • fch - JFileChooser
  • ifr - JInternalFrame
  • lbl - JLabel
  • lyp - JLayeredPane
  • lst - JList
  • mnu - JMenuBar
  • mni - JMenuItem
  • opt - JOptionPane
  • pnl - JPanel
  • pmn - JPopupMenu
  • prg - JProgressBar
  • rad - JRadioButton
  • rot - JRootPane
  • scb - JScollBar
  • scr - JScrollPane
  • spr - JSeparator
  • sld - JSlider
  • spn - JSpinner
  • spl - JSplitPane
  • tab - JTabbedPaneJTable
  • tbl - JTable
  • tbh - JTableHeader
  • txa - JTextArea
  • txt - JTextField
  • txp - JTextPane
  • tgl - JToggleButton
  • tlb - JToolBar
  • tlt - JToolTip
  • tre - JTree
  • vpr - JViewport

source -: http://zuskin.com/java_naming.htm

獨角戲 2024-10-20 09:39:05

我通常讨厌这种东西,因为它有匈牙利记数法的味道。我不喜欢将类型嵌入变量名称的想法,因为如果更改类型,则不应强制更改所有变量名称。

但就 Swing 而言,我想这是可以接受的。

一个好的 IDE 会为你生成变量名。为什么不让呢?如果您坚持的话,我也会拼写出类型(例如 submitButton 而不是 btnSubmit)。按键很便宜。

I usually hate this sort of thing, because it smacks of Hungarian notation. I don't like the idea of embedding the type in the name of the variable, because if you change types it shouldn't mandate changing all the variable names.

But in the case of Swing, I guess it's acceptable.

A good IDE will generate variable names for you. Why not let it? I'd also just spell out the type if you insist (e.g. submitButton instead of btnSubmit). Keystrokes are cheap.

佞臣 2024-10-20 09:39:05

为什么不直接调用 JTextField textField、JButton button、JLabel label 和 JPanel panel。多花几个字符让变量读起来像英文单词有那么糟糕吗?

此外,当我将类型放在变量名中时,我将其放在最后。因此,显示名称的 JLabel 是 nameLabel (IMO 比 lblName 更具可读性)。

而且,根据 duffymo 的建议,将类型放在变量名中是不好的做法。更好的方法是描述变量是什么。对于名称标签,它是一个显示名称的 UI 组件。因此更好的名称可能是 nameComponentnameComponent 是 JLabel 或其他类型的事实是次要的,不应使变量名称混乱。

Why not just call JTextField textField, JButton button, JLabel label, and JPanel panel. Is it so bad to spend a few extra characters to make the variable read like an English word?

Furthermore, when I do put the type in the variable name, I put it on the end. So a JLabel that displays a name is nameLabel (which IMO is more readable than lblName).

And even furthermore, following duffymo's advice, it's bad practice to put the type in the variable name. A better approach is to describe what the variable is. In the case of a name label, it's a UI component that displays name. So a better name might be nameComponent. The fact that nameComponent is a JLabel or some other type is secondary and shouldn't clutter the variable name.

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