クラス内のコンポーネントのリストとそれぞれの変数名を持つことができる必要があります。私は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;
}
変数の名前と型を取得できました...私はこの方法で実装を続けることができると思います。 – ict1991