想法是获取JSplitPane的特定组件,例如:
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
有没有办法从对象panel1
获取panel2
或splitPane
?
答案 0 :(得分:1)
您可以获取容器内所有组件的数组(JSplitPane是一个容器):
// 2 JPanels for example
JPanel panel1 = new JPanel();
panel1.setName("pan1");
getContentPane().add(panel1);
JPanel panel2 = new JPanel();
panel2.setName("pan2");
getContentPane().add(panel2);
// creating the JSplitPane container with those panels
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
// get all components from the JSplitPane and print their names
Component[] components=splitPane.getComponents();
for(Component c:components){
System.out.println(c.getName());
}