是否可以通过某种查找方法获取对 swing 对象的引用??
为每个 UI 元素保留大量实例变量似乎太过分了
即有点像 JavaScript 中的完成方式:
JTextArea ta = new JTextArea();
ta.setId("myJTextArea");
....
....
....
JTextArea ta = window.getElementById("myJTextArea");
ta.setTexT("blah");
PS. 我不是为哥伦比亚号航天飞机编写软件。这是一个快速而肮脏的项目,因此最佳实践不适用。谢谢。
最佳答案
private static Component getComponentById(Container container, String componentId){
if(container.getComponents().length > 0)
{
for(Component c : container.getComponents())
{
if(componentId.equals(c.getName()))
{
return c;
}
if(c instanceof Container)
{
return getComponent((Container) c, componentId);
}
}
}
return null;
}
关于java - 通过 ID 查找 Swing 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5032762/