【发布时间】:2017-08-11 16:52:14
【问题描述】:
我正在创建一个可以在公司内部轻松工作的子网管理器。现在我有了第一个窗口,可以在其中添加新子网 - 使用“添加子网”按钮。这将带您完成几个步骤。提供子网名、网络地址和子网掩码。
添加后,程序将返回到同一启动窗口,但子网已添加到 jList。
现在我正在尝试打开第二个 jFrame,它从概览页面的 JList 中打开选定的子网。将先前输入的值放入第二个 jFrame 的 jTextFields 中时,我遇到了问题。它保持为空,经过调试模式后,我看到我的 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?还是我在监督问题?
【问题讨论】:
-
如需真正的帮助,请考虑发布真正的minimal reproducible example 代码。请阅读链接。作为附带建议,GUI 应该只有一个 JFrame。
-
@HovercraftFullOfEels 在发布之前我已经过滤掉了很多代码。只是想确保类之间的通信清晰。上次我的反应是将更多的代码放到网上。感谢您提供有关 jFrame 的信息。
-
请阅读或重读我给你的链接。
标签: java user-interface arraylist jframe