2012-03-16 48 views
1

我需要能夠有一個類的組件列表和它們各自的變量名稱。我設法得到了GUI中所有組件的列表。但是,當我調用組件的getName方法時,返回null。有誰知道如何能夠獲得這樣一個組件的名稱嗎?如何獲取Java中的GUI組件的名稱?

這是迄今爲止的代碼:

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

回答

1

getName()如果你以前叫setName()只會返回一個值。但是這個名字是任意的。這是UI開發人員的調試幫助。

我的猜測是你想知道哪個組件在Calculator類的哪個字段。爲此,您需要使用Reflection API

嘗試Calculator.class.getDeclaredFields()。要讀取專用字段的值,請參閱this example code

+0

我確實設法獲取變量的名稱和類型......我想我可以繼續以這種方式實現...非常感謝您的幫助 – ict1991 2012-03-16 10:16:01

1

getName()方法爲組件是不能保證返回一個值,而且肯定不返回變量名的部件被分配了。還要注意,可能有多個變量指向同一個對象實例,因此即使信息可用,信息也不會有用。

+0

所以沒有辦法能夠獲得組件的變量名稱? – ict1991 2012-03-16 09:57:46

+0

你不能對你的代碼進行靜態分析,但這是一個非常激進的步驟,我相信是一個簡單的要求。你想達到什麼目的? – Perception 2012-03-16 09:59:14

+0

我正在構建一個工具,最終我需要能夠確定一些我需要突出顯示的組件。所以我需要實際的組件和變量的名稱。起初我也嘗試使用getX和getY來定位組件,但返回零。 – ict1991 2012-03-16 10:05:03

0

每個組件都可以有一個名稱,可以通過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. 
+0

我剛剛添加了代碼 – ict1991 2012-03-16 10:10:30