在我的程序中,我动态生成一个新的文本区域 ta 并使用唯一标识符重命名它(在我的例子中是 tabidis)
JScrollPane panel2 = new JScrollPane();
panel2.setName(tabidis);
ta = new JTextArea("");
ta.setColumns(30);
ta.setRows(20);
ta.setEditable(false);
panel2.setViewportView(ta);
ta.setName(tabidis);
jTabbedPane1.add(username4, panel2);
元素 ta 是动态生成的,并相应地给出相应的名称。
我需要一种方法来获取与所选选项卡相关的“ta”名称
最佳答案
很大程度上取决于用户界面的结构,如果一致,那么它就会变得更容易,如果不一致,就会变得更困难..
您可以使用与此类似的方法来查找从给定父组件开始的给定类型的所有子组件
public static <T extends Component> List<T> findAllChildren(JComponent component, Class<T> clazz) {
List<T> lstChildren = new ArrayList<T>(5);
for (Component comp : component.getComponents()) {
if (clazz.isInstance(comp)) {
lstChildren.add((T) comp);
} else if (comp instanceof JComponent) {
lstChildren.addAll(findAllChildren((JComponent) comp, clazz));
}
}
return Collections.unmodifiableList(lstChildren);
}
这将返回特定类型的子组件的列表
类似...
List<JTextArea> areas = findAllChildren(jTabbedPane1.getSelectedComponent(), JTextArea.class);
if (areas.size() > 0) {
JTextArea ta = areas.get(0);
String name = ta.getName();
}
关于java - 如何获取 jTabbedpane 中文本区域的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17421869/