我使用customButtons作爲Jbutton將的負載容器此構造函數的Java。我獲得通過按鈕名稱(但可通過配置文件):泄漏與NetBeans
getComponent("btn1");
我已經做到了這種方式有按鈕的漂亮的列表,功能的簡單概述,以正確的面板參考「 jpt「來獲得編譯器給我的所有幫助。
按鈕的順序,數量和選擇也需要非編程人員配置 - 我發現按鍵變量名稱的列表,以正常工作)。
的問題是:Netbeans的7.4給我的警告「在構造該泄漏」(9號線),這是真的,因爲我做的。有沒有更正確的方法來達到相同的結果?
public class CustomButtons extends javax.swing.JPanel {
CustomJPanel jpt;
HashMap<String, Component> components;
public CustomButtons (CustomJPanel jpt) {
this.jpt = jpt;
initComponents();
components = getAllComponentsInContainer(this);
}
public Component getComponent(String name) {
return components.get(name);
}
private HashMap<String, Component> getAllComponentsInContainer(Container root) {
HashMap<String, Component> comps = new HashMap<>();
for (Component c : root.getComponents()) {
comps.put(c.getName(), c);
if (c instanceof Container) {
comps.putAll(getAllComponentsInContainer((Container) c));
}
}
return comps;
}
你的類名應該以大寫字母開頭 - 它使識別類和變量更容易。 – Njol
謝謝,這是一個匿名化神器,修復。 – user3218163