问题
Is there anyway to add an ID or a unique identifier to a JLabel?
Thanks all
回答1:
JLabel has a setName method that is inherited from java.awt.Component. You could use this for an ID.
回答2:
You can call Component.setName
on it, or JComponent.putClientProperty
, or subclass or use the JLabel
object as a key is a Map
of some sort.
But probably the way to go is to keep hold of a reference to the original object, so you can write clean, direct code that doesn't have to look the component up.
来源:https://stackoverflow.com/questions/590473/java-jlabel-add-a-unique-identifier