Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中获取GUI组件的名称?_Java_User Interface - Fatal编程技术网

如何在Java中获取GUI组件的名称?

如何在Java中获取GUI组件的名称?,java,user-interface,Java,User Interface,我需要能够有一个类中的组件列表和它们各自的变量名。我确实设法得到了GUI中所有组件的列表。但是,当我调用组件的getName方法时,返回一个null。有人知道如何为一个组件命名吗 这是迄今为止的代码: public static void main(String[] args){ Calculator c = new Calculator(); List<Component> containers = getAllComponents(c.getContentPan

我需要能够有一个类中的组件列表和它们各自的变量名。我确实设法得到了GUI中所有组件的列表。但是,当我调用组件的getName方法时,返回一个null。有人知道如何为一个组件命名吗

这是迄今为止的代码:

public static void main(String[] args){
    Calculator c = new Calculator();

    List<Component> containers = getAllComponents(c.getContentPane());
    for (int i = 0; i < containers.size(); i++) {
        System.out.println(containers.get(i).getClass().getName());
        System.out.println(containers.get(i).getName());
    }
    System.exit(0);
}

public static List<Component> getAllComponents(final Container c) {
    Component[] comps = c.getComponents();
    List<Component> compList = new ArrayList<Component>();
    for (Component comp : comps) {
        compList.add(comp);
        if (comp instanceof Container) {
            compList.addAll(getAllComponents((Container) comp));
        }
    }
    return compList;
}
publicstaticvoidmain(字符串[]args){
计算器c=新计算器();
List containers=getAllComponents(c.getContentPane());
对于(int i=0;i
组件的
getName()
方法不能保证返回值,当然也不能返回组件分配的变量名。还请注意,多个变量可能指向同一个对象实例,因此即使信息可用,也没有什么用处。

每个组件都可以有一个名称,通过getName()和setName()访问,但您必须编写自己的查找函数

您也可以尝试使用HashMap。我发现了一个例子

    Create a Map class variable. You'll need to import HashMap at the very least. I named mine componentMap for simplicity.

private HashMap componentMap;

    Add all of your components to the frame as normal.

initialize() {
    //add your components and be sure
    //to name them.
    ...
    //after adding all the components,
    //call this method we're about to create.
    createComponentMap();
}

    Define the following two methods in your class. You'll need to import Component if you haven't already:

private void createComponentMap() {
        componentMap = new HashMap<String,Component>();
        Component[] components = yourForm.getContentPane().getComponents();
        for (int i=0; i < components.length; i++) {
                componentMap.put(components[i].getName(), components[i]);
        }
}

public Component getComponentByName(String name) {
        if (componentMap.containsKey(name)) {
                return (Component) componentMap.get(name);
        }
        else return null;
}

    Now you've got a HashMap that maps all the currently existing components in your frame/content pane/panel/etc to their respective names.

    To now access these components, it is as simple as a call to getComponentByName(String name). If a component with that name exists, it will return that component. If not, it returns null. It is your responsibility to cast the component to the proper type. I suggest using instanceof to be sure.
创建映射类变量。您至少需要导入HashMap。为了简单起见,我将我的组件命名为Map。
私有HashMap组件映射;
将所有组件正常添加到框架中。
初始化(){
//添加组件并确保
//请说出他们的名字。
...
//在添加所有组件之后,
//调用我们将要创建的方法。
createComponentMap();
}
在类中定义以下两个方法。如果尚未导入组件,则需要导入组件:
私有void createComponentMap(){
componentMap=新的HashMap();
Component[]components=yourForm.getContentPane().getComponents();
对于(int i=0;i
getName()
只有在之前调用了
setName()
时才会返回值。但这个名字是任意的。它是UI开发人员的调试辅助工具

我的猜测是,您想知道哪个组件位于
计算器
类的哪个字段中。为此,您需要使用


尝试
Calculator.class.getDeclaredFields()
。要读取私有字段的值,请参见。

因此没有办法获取组件的变量名称?您不能对代码执行静态分析,但这是一个非常激进的步骤,因为我相信这是一个简单的要求。你想实现什么?我正在构建一个工具,最终我需要能够识别一些我需要强调的组件。所以我需要实际的组件和变量的名称。起初,我还尝试使用getX和getY来定位组件,但是返回了一个零。您总是可以获得对象实例,但是组件只有在(由开发人员或框架)分配了一个名称时才会有名称。正如我所说,变量名在运行时是不可用的。设想一个完全在超出范围的方法上下文中创建的UI。不再有指向这些组件的“变量”,只有堆上的引用。我确实设法获得了变量的名称和类型。。。我想我将能够以这种方式继续我的实施。。。非常感谢你的帮助