簡體   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