空ArrayList打开新的jFrame时
我正在创建一个可以在公司内轻松工作的子网管理器。现在我有了第一个窗口,可以添加新的子网 - 使用“添加子网”按钮。这需要你通过几个步骤。给予子网名称,网络地址和子网掩码。空ArrayList打开新的jFrame时
一旦添加程序就返回到这个相同的起始窗口,但子网添加到jList。
现在我试图打开第二个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)
现在两个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:
代码逻辑类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?还是我监督这个问题?
修复了这个问题。在线路:
private SubnetManager n = new SubnetManager();
private Subnet s = new Subnet();
Slibbed在我脑海中,当你使用“新”它会创建一个新的对象。所以我通过在我的逻辑类Subnet.java中添加gettermethod并删除这两行来修复它。
对于真正的帮助,请考虑发布真正的[mcve]代码。请阅读链接。作为一个侧面推荐,GUI应该只有一个JFrame。 –
@HovercraftFullOfEels在发布之前,我已经过滤了大量代码。只是想确保班级之间的沟通是清楚的。上次我有反应将更多的代码放在网上。并感谢您关于jFrame的信息。 – Lorenzo
请阅读或重新阅读我给你的链接。 –