java.awt.Component.getName() 和 setName() 的用途是什么?
什么是 java.lang. awt.Component.getName()
用于什么? 在我使用 NetBeans 构建的应用程序中,它似乎始终为 null
。 我正在考虑在其中存储每个组件的一些帮助文本 - 我不想使用工具提示,我有另一个面板,我将在其中显示帮助文本。
What is java.awt.Component.getName()
used for? It always seems to be null
in the applications I build with NetBeans. I'm thinking of storing some help text per component in it -- I don't want to use the tooltip, I have another panel where I'll show the help text.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Component.setName(..) 在 JDK 中主要由外观实现类使用来为每个组件设置类似于 ID 的字符串,例如 BasicOptionPaneUI 可能会在按钮组件上调用它以将其名称设置为“OptionPane.button”。
当设置复合/父组件内的子组件名称以及 AWT 和 Swing 调试日志记录代码中时, getName() 在 toString() 方法中使用。
我强烈怀疑 getName() 方法也被某些 AWT/Swing 测试框架使用。
因此,如果您不依赖 getName() 的上述任何用法,您可以尝试将其用于帮助消息,但我不推荐它。
也许你应该重新考虑你的设计? 使用该名称在哈希图中进行一些查找,以从资源包加载帮助文本?
Component.setName(..) is used in the JDK mostly by the look and feel implementation classes to set ID-like strings for each component, e.g. BasicOptionPaneUI might call it on a button component to set its name to "OptionPane.button".
The getName() is used in toString() methods, when setting the names of child components inside a Composite/parent Component and in AWT and Swing debug logging code.
I suspect strongly that the getName() method is also used by some AWT/Swing testing frameworks.
So if you're not dependent on any of the above uses of getName(), you might try using it for your help messages, though I would not recommend it.
Maybe you should reconsider your design? Use the name to do some lookup in a hashmap that loads the help text from a resource bundle?
我还没有看到框架将它用于任何用途。 如果您将组件传递给方法,那么它很有用,这样您就可以询问它们的名称来决定如何处理它们。 此外,许多 UI 测试框架使用它来允许您在测试脚本中按名称引用组件。 不过,我看不出有什么理由不能将它用于帮助文本。
I haven't seen it used for anything by the framework. Its useful if you have components being passed in to a method so you can ask their name to decide how to handle them. Also, many UI testing frameworks use this to allow you to refer to the components by name in the testing scripts. I don't see any reason you can't use it for help text though.
Herman Lintvelt 的答案最终成为我的应用程序的正确答案。
我创建了一个名为 HelpText.properties 的资源包。 它包含名称=值对。 我使用名称=值对中的“名称”对每个组件进行 setName()d。 然后我使用框架的 getGlassPane() 来捕获所有鼠标移动。 当鼠标滑过指定组件时,它会在包中查找名称,显示帮助(如果可用),并将鼠标运动转发到实际组件。
呼。 只有2天的时间去吃饭。 我终于开始习惯 Java 了:)
Herman Lintvelt's answer ended up being the correct one for my app.
I created a resource bundle named HelpText.properties. It contains name=value pairs. I setName()d each of my Components with the "name" from the name=value pair. I then used a the frame's getGlassPane() to capture all mouse movements. When a mouse runs over a named component, it looks up the name in the bundle, displays help if available and forwards the mouse motion to along to the actual Component.
Whew. Only 2 days worth of dinking around. I'm finally starting to get used to Java :)
FEST 使用组件的名称在测试用例中识别它。
FEST uses the name of a Component to identify it in testcases.
component.getName()
方法主要与侦听器一起使用。 如果您设置组件的名称 (component.setName(name)
),您可以从Listener
的方法中调用该特定组件。示例:
请注意,您必须显式设置组件的名称,否则将返回
null
。The
component.getName()
method is mostly used with listeners. If you set the name of a component (component.setName(name)
) you can call to that specific component from within a method of aListener
.Example:
Be aware that you have to explicitly set the name of the component, otherwise it will return
null
.另外,由于我认为 java.awt.Component 是 X 中的重量级对象,因此像 xwininfo 和 xwd 这样的程序可能允许您通过名称指定它。
我只是用 JFrame 尝试了一下,setName 没有设置窗口的名称,窗口是由我在构造函数中传递的字符串命名的。 但我没有任何仅 awt 的示例代码可供测试,因此我上面写的内容可能是错误的。
Also, since I think java.awt.Component is a heavyweight object in X, programs like xwininfo and xwd might allow you to specify it by name.
I just tried it with a JFrame, and setName didn't set the name of the window, the window was named by the string I passed in the constructor. But I don't have any awt-only example code to test with, so I could be wrong about what I wrote above.
我用它来处理一个单独的类中的侦听器。 我接收包含 object.addListener 的组件作为参数,不是作为容器,而是作为包含该对象的类。 感谢 Vivavinyl 提供的建议,首先设置名称。 它很有用并且有效。
I use it for handling listeners in one single class apart. I receive as a parameter the component which contains my object.addListener not as a container but as the class that contains that object. Thanks Vivavinyl for the the tip of setting the name first. It was useful and worked.
这就是我使用 getName() 的目的:
This is what I use getName() for:
我搜索了很多答案来获取名字
我认为这是唯一简单的解决方案
I have searched many answers for getting name
and i think this is the only easy solution