java.awt.Component.getName() 和 setName() 的用途是什么?

发布于 2024-07-08 06:30:11 字数 280 浏览 10 评论 0原文

什么是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

怀念你的温柔 2024-07-15 06:30:11

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?

比忠 2024-07-15 06:30:11

我还没有看到框架将它用于任何用途。 如果您将组件传递给方法,那么它很有用,这样您就可以询问它们的名称来决定如何处理它们。 此外,许多 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.

寒尘 2024-07-15 06:30:11

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 :)

躲猫猫 2024-07-15 06:30:11

FEST 使用组件的名称在测试用例中识别它。

FEST uses the name of a Component to identify it in testcases.

哥,最终变帅啦 2024-07-15 06:30:11

component.getName() 方法主要与侦听器一起使用。 如果您设置组件的名称 (component.setName(name)),您可以从 Listener 的方法中调用该特定组件。

示例:

public void someMethodOfsomeListener(SomeEvent e){
   if (e.getComponent().getName().equals(component.getName())
      //do stuff...
}

请注意,您必须显式设置组件的名称,否则将返回 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 a 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.

从﹋此江山别 2024-07-15 06:30:11

另外,由于我认为 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.

心奴独伤 2024-07-15 06:30:11

我用它来处理一个单独的类中的侦听器。 我接收包含 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.

音盲 2024-07-15 06:30:11

这就是我使用 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;

        }

    }

This is what I use getName() for:

    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;

        }

    }
柠檬色的秋千 2024-07-15 06:30:11

我搜索了很多答案来获取名字
我认为这是唯一简单的解决方案

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文