Java Swing 组件的命名约定(前缀)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4770174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Name convention on Java Swing components(prefix)
提问by LuckyLuke
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.
在 Java 中使用 Swing 进行编程时,我想到的一个问题是,它是 Swing 组件的推荐还是“官方”/最常用的命名约定(前缀)。
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
- JTextField 的 txt
- JButton 的 btn
- JLabel 的 lbl
- JPanel 的 pnl
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.
我认为这样的前缀增强了我的代码的可读性,但是我没有任何组件的名称,例如 JComboBox、JList、JRadioButton、JCheckButton 等等。
Thanks in advance.
提前致谢。
回答by Sandman
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.
人们会告诉你使用前缀是不好的,因为那是匈牙利表示法,而现在,匈牙利表示法被认为是编程中的一大禁忌。就我个人而言,作为做过一定数量 GUI 工作的人,我可以告诉你,虽然在非 GUI 编程中绝对应该避免使用匈牙利符号,但在进行 GUI 时这是一个非常好的做法。
例如,考虑一个带有文本框的简单表单,用于输入用户名。在这个文本框前面应该是一个标签,提示用户在文本框中输入他的名字。现在,您将如何命名文本框?'名称'?标签呢?一个好的做法应该是在文本框前面加上 txt,然后用 label 加上 label,这就是匈牙利符号的全部意义所在。因此,文本框现在命名为“txtName”,相应的标签命名为“lblName”。这为您提供了在 IDE 编辑器中轻松访问文本框、组合框和其他小部件的额外好处。例如,如果您遵循此表示法,在 Eclipse 中输入“txt”并按 CTRL+Space 将打开一个上下文菜单,列出所有文本框。
现在,回答你的问题。定义前缀应该使用哪三个字母的常用方法是从小部件名称中删除所有元音以及所有重复的辅音。如果剩余的辅音超过三个,则应忽略它们。因此,文本框(或 TextField,或您首选的小部件工具包中的任何此小部件)将变为“txt”、标签“lbl”、组合框“cmb”、表格“tbl”等等。
回答by duffymo
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.
但就 Swing 而言,我想这是可以接受的。
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.
一个好的 IDE 会为你生成变量名。为什么不让它?如果您坚持(例如,submitButton
而不是btnSubmit
),我也会拼出类型。按键很便宜。
回答by Steve Kuo
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?
为什么不直接调用 JTextField textField
、 JButton button
、 JLabellabel
和 JPanel panel
。花几个额外的字符让变量读起来像一个英文单词是不是很糟糕?
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
).
此外,当我将类型放在变量名中时,我把它放在最后。因此,显示名称的 JLabel 是nameLabel
(IMO 比 更具可读性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.
此外,按照 duffymo 的建议,将类型放在变量名中是不好的做法。更好的方法是描述变量是什么。对于名称标签,它是一个显示名称的 UI 组件。所以更好的名字可能是nameComponent
. nameComponent
JLabel 或其他类型的事实是次要的,不应混淆变量名称。
回答by Jamith
- 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
- 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
- 选择 - JOptionPane
- pnl - JPanel
- pmn - JPopupMenu
- prg - JProgressBar
- rad - JRadioButton
- 腐烂 - JRootPane
- scb - JScollBar
- scr - JScrollPane
- spr - JS分隔符
- sld - JSlider
- spn - JSpinner
- spl - JSplitPane
- 选项卡 - 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
来源 -:http: //zuskin.com/java_naming.htm