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 J开发者_运维技巧Button
- 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.
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.
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.
- 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
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.
精彩评论