繁体   English   中英

访问并创建Swing GUI元素树

[英]Access and create a tree of Swing GUI elements

我的问题是如何访问Swing GUI元素树(主窗口,JPanel,JFrames,JButton,JTextFields ect)并创建对该树的引用。 我需要将其保存在数据结构(例如哈希映射)中,而不要保存在内存文件中(例如,使用序列化)。 我需要使用它,以便以后将这些UI元素映射到代码内的相应对象。

编辑:

   JFrame f = new JFrame("Basic GUI"); 
   JPanel pnl1 = new JPanel(); 
   JPanel pnl2 = new JPanel(); 
   JPanel pnl3 = new JPanel(); 

   JLabel lblText = new JLabel("Test Label");
   JButton btn1 = new JButton("Button");
   JTextField txtField = new JTextField(20);

   public GUISample(){

   pnl1.add(lblText);
   pnl2.add(btn1);
   pnl3.add(txtField);

   f.getContentPane().setLayout(new BorderLayout());
   f.getContentPane().add(pnl2, BorderLayout.EAST);
   f.getContentPane().add(pnl3, BorderLayout.WEST);
   f.getContentPane().add(pnl1, BorderLayout.NORTH);
   visitComponent(f);

   }

   private Map<String, Component> hashMap = new HashMap<String,Component>();

   public Map<String, Component> getComponentsTree(){
      return hashMap;
   }
   public void visitComponent(Component cmp){
      // Add this component
      if(cmp != null) hashMap.put(cmp.getName(), cmp);
      Container container = (Container) cmp;
      if(container == null ) {
          // Not a container, return
          return;
      }
      // Go visit and add all children
      for(Component subComponent : container.getComponents()){
          visitComponent(subComponent);
      }
     System.out.println(hashMap); 

    }

查看Darryl的组件树模型

我一直在考虑这个问题。 这是我的建议:

public class ComponentTreeBuilder{
   private Map<String, Component> hashMap = new HashMap<String,Component>();
   public Map<String, Component> getComponentsTree(){
      return hashMap;
   }
   public void visitComponent(Component cmp){
      // Add this component
      if(cmp != null) hashMap.put(cmp.getName(), cmp);
      Container container = (Container) cmp;
      if(container == null ) {
          // Not a container, return
          return;
      }
      // Go visit and add all children
      for(Component subComponent : container.getComponents()){
          visitComponent(subComponent);
      }
   }
}

像这样使用:

Frame myFrame = new JFrame();
// Make sure you add your elements into the frame's content pane by
myFrame.getContentPane(component);
ComponentTreeBuilder cmpBuilder = new ComponentTreeBuilder();
cmpBuilder.visitComponent(myFrame);
Map<String, Component> components = cmpBuilder.getComponentsTree(); 
// All components should now be in components hashmap

请注意, ComponentTreeBuilder正在使用递归,如果GUI中的组件过多,则可能会引发堆栈溢出异常。

编辑刚刚在实际示例上测试了此代码,并且....它起作用:)

这是代码:

JFrame frame = new JFrame();
frame.getContentPane().add(new JButton());
frame.getContentPane().add(new JButton());
frame.getContentPane().add(new JButton());
frame.getContentPane().add(new JButton());
visitComponent(frame);

这是输出:

在此处输入图片说明

从命令行启动程序,并输入控制 + Shift + F1,以查看当前摆容器层次的转储,如图所示这里 它不是很可读,但是缩进是层次结构的可靠反映。

附录:以这种方式获得的结果可用作评估程序实现的标准。 作为参考,我对照自己运行了Darryl的ComponentTree ,并将键盘结果与JTree的剪切和粘贴副本进行了比较。 仅有细微的差异出现:

  1. 转储从封闭的JFrame开始,而树从框架的根窗格开始。

  2. 转储按词法缩进,而树则直接对层次进行建模。

  3. 转储包括CellRendererPane实例(标记为hidden) ,由JTable呈现实现使用; 树没有。

忽略这些,两者的组件顺序是相同的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM