2017-08-11 77 views
1

我正在創建一個可以在公司內輕鬆工作的子網管理器。現在我有了第一個窗口,可以添加新的子網 - 使用「添加子網」按鈕。這需要你通過幾個步驟。給予子網名稱,網絡地址和子網掩碼。空ArrayList打開新的jFrame時

一旦添加程序就返回到這個相同的起始窗口,但子網添加到jList。 Homepage Subnet Manager

現在我試圖打開第二個jFrame,它從概覽頁面打開JList中選定的子網。在第二個jFrame的jTextField中放入先前輸入的值是有問題的。它保持爲空,並且在通過調試模式之後,我看到我的ArrayList在打開第二個jFrame後再次爲空。

在異常線程 「AWT-EventQueue的-0」 java.lang.IndexOutOfBoundsException:索引:0,大小:0 在java.util.ArrayList.rangeCheck(ArrayList.java:653) 在java.util中.ArrayList.get(ArrayList.java:429)

JFrame selected Subnet

現在兩個GUI的都是單獨的Java文件(SubnetManager.java & SubnetGUI.java)。這是由包含邏輯方法的類Subnet(.java)所幫助的。

代碼SubnetManager.java:

Subnet n = new Subnet(); 
private DefaultListModel model = new DefaultListModel(); 
private int getal; 

/** 
* Creates new form SubnetManager 
*/ 
public SubnetManager() { 
    initComponents(); 
}  

/** 
* Opens up the jFrame of SubnetGUI.java with the values of the selected jList value. 
* 
* @param evt Clickevent 'Change' Button 
*/ 
private void jButtonChangeSubnetActionPerformed(java.awt.event.ActionEvent evt) {              
    //integer 'getal' gets the index of the selected jList value 
    getal = jListSubnet.getSelectedIndex(); 

    //closes jFrame from SubnetManagerGUI 
    this.setVisible(false); 

    //Opens up the jFrame from SubnetGUI 
    SubnetGUI s = new SubnetGUI(); 
    s.setVisible(true); 
}             

/** 
* Gettermethod which provides the index of the selected JList value. 
* 
* @return index of selected JList value 
*/ 
public int getGetal() { 
    return this.getal; 
} 

代碼SubnetGUI.java:

private SubnetManager n = new SubnetManager(); 
private Subnet s = new Subnet(); 
private String getal2; 

/** 
* Creates new form Subnet 
*/ 
public SubnetGUI() { 
    initComponents(); 
    this.getal2 = s.naamSubnet.get(n.getGetal()); 
} 

草簽選擇的子網的JTextField中(名稱) - SubnetGUI.java: SubnetGUI initizialing jTextFieldName

代碼邏輯類Subnet.java:

public class Subnet { 
private String naam, netwerkadres, subnetmask; 

//ArrayList which saves the names of the Subnets 
public final ArrayList<String> naamSubnet = new ArrayList<String>(); 

/** 
* Constructor 
*/ 
public Subnet() { 

} 

/** 
* Constructor 
* @param naam name of subnet 
* @param netwerkadres network address of subnet 
* @param subnetmask subnetmask of subnet 
*/ 
public Subnet(String naam, String netwerkadres, String subnetmask) { 
    this.naam = naam; 
    this.netwerkadres = netwerkadres; 
    this.subnetmask = subnetmask; 
} 

/** 
* Method to add names of subnets to the ArrayList naamSubnet. 
* 
* @param antwoord name provided in SubnetManager GUI 
*/ 
public void naamSubnet(String antwoord) { 
    naamSubnet.add(antwoord); 
} 

}

現在我的問題是,當第二個jFrame被打開時,ArrayList是否可以爲空?程序是否重置,是否需要在SubnetManager.java類中打開一個新的jFrame?還是我監督這個問題?

+0

對於真正的幫助,請考慮發佈真正的[mcve]代碼。請閱讀鏈接。作爲一個側面推薦,GUI應該只有一個JFrame。 –

+0

@HovercraftFullOfEels在發佈之前,我已經過濾了大量代碼。只是想確保班級之間的溝通是清楚的。上次我有反應將更多的代碼放在網上。並感謝您關於jFrame的信息。 – Lorenzo

+0

請閱讀或重新閱讀我給你的鏈接。 –

回答

1

修復了這個問題。在線路:

private SubnetManager n = new SubnetManager(); 
private Subnet s = new Subnet(); 

Slibbed在我腦海中,當你使用「新」它會創建一個新的對象。所以我通過在我的邏輯類Subnet.java中添加gettermethod並刪除這兩行來修復它。