java.awt.Component.getName() 和 setName() 有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/227143/
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
What is java.awt.Component.getName() and setName() used for?
提问by JohnnyLambada
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.
是什么java.awt.Component.getName()
用的?它似乎总是出现null
在我使用 NetBeans 构建的应用程序中。我正在考虑在其中为每个组件存储一些帮助文本——我不想使用工具提示,我有另一个面板,我将在其中显示帮助文本。
采纳答案by Herman Lintvelt
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".
Component.setName(..) 在 JDK 中主要由外观实现类用于为每个组件设置类似 ID 的字符串,例如 BasicOptionPaneUI 可能会在按钮组件上调用它以将其名称设置为“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.
getName() 在 toString() 方法中使用,当在复合/父组件内以及在 AWT 和 Swing 调试日志代码中设置子组件的名称时。我强烈怀疑 getName() 方法也被一些 AWT/Swing 测试框架使用。
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.
因此,如果您不依赖于上述任何 getName() 用法,您可以尝试将它用于您的帮助消息,尽管我不推荐它。
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?
也许你应该重新考虑你的设计?使用名称在从资源包加载帮助文本的哈希图中进行一些查找?
回答by AdamC
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.
我还没有看到它被框架用于任何东西。如果您将组件传递给方法,则它很有用,因此您可以询问它们的名称来决定如何处理它们。此外,许多 UI 测试框架使用它来允许您在测试脚本中按名称引用组件。不过,我看不出有任何理由不能将它用于帮助文本。
回答by Paul Tomblin
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.
另外,由于我认为 java.awt.Component 是 X 中的重量级对象,因此 xwininfo 和 xwd 之类的程序可能允许您按名称指定它。
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.
我只是用 JFrame 试了一下,setName 并没有设置窗口的名称,窗口是由我在构造函数中传递的字符串命名的。但是我没有任何仅用于测试的 awt 示例代码,所以我上面写的内容可能是错误的。
回答by JohnnyLambada
Herman Lintvelt's answer ended up being the correct one for my app.
Herman Lintvelt 的答案最终成为我的应用程序的正确答案。
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.
我创建了一个名为 HelpText.properties 的资源包。它包含名称=值对。我使用 name=value 对中的“name” setName()d 我的每个组件。然后我使用框架的 getGlassPane() 来捕获所有鼠标移动。当鼠标在命名组件上运行时,它会在包中查找名称,显示帮助(如果可用)并将鼠标移动转发到实际组件。
- Look herefor how to use the glass pane.
- Jan Newmarch's web page on using resource bundleswas very helpful.
- Kevin Riff's responsehelped me figure out how to find my resource bundle.
Whew. Only 2 days worth of dinking around. I'm finally starting to get used to Java :)
哇。只有 2 天的用餐时间。我终于开始习惯 Java 了 :)
回答by JohnnyLambada
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 a Listener
.
该component.getName()
方法主要用于侦听器。如果您设置组件的名称 ( component.setName(name)
),您可以从Listener
.
Example:
例子:
public void someMethodOfsomeListener(SomeEvent e){
if (e.getComponent().getName().equals(component.getName())
//do stuff...
}
Be aware that you have to explicitly set the name of the component, otherwise it will return null
.
请注意,您必须明确设置组件的名称,否则它将返回null
.
回答by JohnnyLambada
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.
我用它来处理一个单独的类中的听众。我将包含 object.addListener 的组件作为参数接收,而不是作为容器,而是作为包含该对象的类。感谢 Vivavinyl 首先设置名称的提示。它很有用并且有效。
回答by JohnnyLambada
This is what I use getName() for:
这就是我使用 getName() 的目的:
Frame[] frames = JFrame.getFrames();
for (int i = 0; i < frames.length; ++i) {
//get the frame
Frame frame = frames[i];
if (frame.getName().equals(frameName)) {
//make the frame visible
frame.setVisible(true);
//focus the frame
frame.requestFocus();
//found
return;
}
}
回答by Tural
I have searched many answers for getting name and i think this is the only easy solution
我已经搜索了很多获得名字的答案,我认为这是唯一简单的解决方案
public static void main(String[] args) {
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
String name = actionEvent.getSource().toString();
UserReaction(ObjectName.getComponentVariableName(name), "null");
}
};
Button calculate_btn = new Button("Calculate");
calculate_btn.setName("Calculate");
calculate_btn.addActionListener(actionListener);
}
private static void UserReaction(String objectName) {
if (objectName.equals("Calculate")) {
//do something;
}
}static public String getComponentVariableName(String name) {
String res = (name.substring(name.indexOf("[") + 1));
res = res.split(",")[0];
return res;
}