简体   繁体   English

Java JLabel - 添加唯一标识符

[英]Java JLabel - add a unique identifier

Is there anyway to add an ID or a unique identifier to a JLabel? 无论如何都要为JLabel添加ID或唯一标识符?

Thanks all 谢谢大家

JLabel has a setName method that is inherited from java.awt.Component. JLabel有一个从java.awt.Component继承的setName方法。 You could use this for an ID. 您可以将此用作ID。

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. 您可以在其上调用Component.setName ,或者调用JComponent.putClientProperty或子类,或者使用JLabel对象作为键,是某种类型的Map

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. 但可能的方法是保持对原始对象的引用,这样您就可以编写干净,直接的代码,而不必查看组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM