访问并创建Swing GUI元素树 [英] Access and create a tree of Swing GUI elements

查看:102
本文介绍了访问并创建Swing GUI元素树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My question is how to access the Swing GUI element tree (main window, JPanels, JFrames, JButtons, JTextFields ect) and create a reference to that tree. I need this to be kept in a data structure (ex. hash map) and NOT in a memory file (eg. using serialization). I need this for using it later to map these UI elements to the corresponding objects inside the code.

编辑:

   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); 

    }


推荐答案

我有一直在想这个问题。以下是我的建议:

I have been thinking about this problem. Here is my suggestion:

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中有太多组件,这可能会引发堆栈溢出异常。

Please note that ComponentTreeBuilder is using recursion, this might throw a stack overflow exception if you have too many components in your GUI.

编辑刚刚在真实示例中对此代码进行了测试......它的工作原理是:)

EDIT Just tested this code on real example and.... it works :)

以下是代码:

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);

这是输出:

这篇关于访问并创建Swing GUI元素树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆