我正在尝试根据用户提供的输入动态生成标签及其包含的文本字段。输入在所示代码中为noTCPFlow。但是当我运行代码时,我一直得到这个例外:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at javax.swing.JTabbedPane.setTitleAt(JTabbedPane.java:1316)
at myproject.ns2GUI.trafficInfoInit(ns2GUI.java:622)
at myproject.ns2GUI.flowDetailsContinueActionPerformed(ns2GUI.java:567)
at myproject.ns2GUI.access$200(ns2GUI.java:16)
at myproject.ns2GUI$3.actionPerformed(ns2GUI.java:343)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
屏幕上没有显示任何内容。
我已经展示了以下代码。有人可以帮忙吗?
package myproject;
import java.awt.*;
import java.awt.CardLayout;
import javax.swing.*;
public class ns2GUI extends javax.swing.JFrame {
private CardLayout laycard;
public int noTCPFlows,noUDPFlows,totalFlows;
public ns2GUI() {
initComponents();
laycard = new CardLayout();
mainPanel.setLayout(laycard);
mainPanel.add("scenarioParameters",scenarioParameters);
mainPanel.add("nodeVisualization",nodeVisualization);
mainPanel.add("flowDetails",flowDetails);
mainPanel.add("trafficInfo",trafficInformation);
laycard.show(mainPanel,"scenarioParameters");
}
public void goScenarioParams() {
laycard.show(mainPanel,"scenarioParameters");
}
public void goNodeVisualization() {
laycard.show(mainPanel,"nodeVisualization");
}
public void goFlowDetails() {
laycard.show(mainPanel,"flowDetails");
}
public void goTrafficInfo() {
laycard.show(mainPanel,"trafficInfo");
}
private void scenarioParamsClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
simTime.setText(" ");
noNodes.setText(" ");
tSizeX.setText(" ");
tSizeY.setText(" ");
initEnergy.setText(" ");
transmitPower.setText(" ");
recPower.setText(" ");
}
private void scenarioParamsOkActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
goFlowDetails();
}
private void enterDetailsActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
srcSinkPanel.removeAll();
srcSinkPanel.setLayout(new GridLayout(0,1));
noTCPFlows = Integer.parseInt(noTcpFlows.getText());
noUDPFlows = Integer.parseInt(noUdpFlows.getText());
totalFlows = noTCPFlows + noUDPFlows;
String tcpLabel = "TCP Flow ";
String udpLabel = "UDP Flow ";
String source = "source";
String sink = "sink";
String panel = "panel";
int gridSize = 3;
int i = 0;
//loop for TCP flows
for(int k=0;k<noTCPFlows; k++) {
JLabel jL = new JLabel(tcpLabel + k);
JTextField jT1 = new JTextField(source + k,10);
JTextField jT2 = new JTextField(sink + k,10);
jT1.setText(" ");
jT2.setText(" ");
String panelName = panel + i++;
JPanel jP = new JPanel();
jP.add(jL);
jP.add(jT1);
jP.add(jT2);
jP.setLayout( new GridLayout( 1, gridSize ) );
srcSinkPanel.add(jP);
}
//loop for UDP flows
for(int j=0;j<noUDPFlows; j++) {
JLabel jL = new JLabel(udpLabel + j);
JTextField jT1 = new JTextField(source + j,10);
JTextField jT2 = new JTextField(sink + j,10);
jT1.setText(" ");
jT2.setText(" ");
String panelName = panel + i++;
JPanel jP = new JPanel();
jP.add(jL);
jP.add(jT1);
jP.add(jT2);
jP.setLayout( new GridLayout( 1, gridSize ) );
srcSinkPanel.add(jP);
}
//add the Source Sink panel
srcSinkPanel.revalidate();
}
private void flowDetailsBackActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
goScenarioParams();
}
private void flowDetailsContinueActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
goTrafficInfo();
trafficInfoInit();
}
public void trafficInfoInit() {
String tabName = "tab";
String tcpLabel = "TCP Flow";
String udpLabel = "UDP Flow ";
String[] flowType = {"CBR","FTP","Exponential"};
int countTcp = 1;
int countUdp = 1;
//Main Panel contents
JPanel[] jP = new JPanel[noTCPFlows];
JLabel[] jL =new JLabel[noTCPFlows];
JComboBox[] jCb = new JComboBox[noTCPFlows];
//option panels
JPanel[] cbrParams = new JPanel[noTCPFlows];
JPanel[] expoParams = new JPanel[noTCPFlows];
JPanel[] ftpParams = new JPanel[noTCPFlows];
//CBR Contents
JLabel[] jL1Cbr = new JLabel[noTCPFlows];
JLabel[] jL2Cbr = new JLabel[noTCPFlows];
JLabel[] jL3Cbr = new JLabel[noTCPFlows];
JLabel[] jL4Cbr = new JLabel[noTCPFlows];
JTextField[] cbrPacketSize = new JTextField[noTCPFlows];
JTextField[] cbrRate = new JTextField[noTCPFlows];
JTextField[] cbrInterPacketInt = new JTextField[noTCPFlows];
JTextField[] cbrMaxPackets = new JTextField[noTCPFlows];
//Expo contents
JLabel[] jL1Expo = new JLabel[noTCPFlows];
JLabel[] jL2Expo = new JLabel[noTCPFlows];
JLabel[] jL3Expo = new JLabel[noTCPFlows];
JLabel[] jL4Expo = new JLabel[noTCPFlows];
JTextField[] expoPacketSize = new JTextField[noTCPFlows];
JTextField[] expoRate = new JTextField[noTCPFlows];
JTextField[] expoBurstTime = new JTextField[noTCPFlows];
JTextField[] expoIdealTime = new JTextField[noTCPFlows];
//FTP Contents
JLabel[] jL1Ftp = new JLabel[noTCPFlows];
JLabel[] jL2Ftp = new JLabel[noTCPFlows];
JTextField[] ftpMaxPackets = new JTextField[noTCPFlows];
JTextField[] ftpMoreCountPackets = new JTextField[noTCPFlows];
JPanel[] jP2 = new JPanel[noTCPFlows];
CardLayout[] layPanelCard = new CardLayout[noTCPFlows];
for( int i = 0; i<noTCPFlows; i++) {
//create a new tab & set title
jP[i] = new JPanel();
jP[i].setLayout(new GridLayout(2,2));
countTcp++;
//create the flow type label and combo box
jL[i] = new JLabel("Flow Type:");
jCb[i] = new JComboBox(flowType);
//create the options panels
//CBR Panel
cbrParams[i] = new JPanel();
jL1Cbr[i] = new JLabel("Packet Size");
cbrPacketSize[i] = new JTextField();
jL2Cbr[i] = new JLabel("Rate");
cbrRate[i] = new JTextField();
jL3Cbr[i] = new JLabel("Inter Packet Interval");
cbrInterPacketInt[i] = new JTextField();
jL4Cbr[i] = new JLabel("Maximum Packets");
cbrMaxPackets[i] = new JTextField();
cbrParams[i].add(jL1Cbr[i]);
cbrParams[i].add(cbrPacketSize[i]);
cbrParams[i].add(jL2Cbr[i]);
cbrParams[i].add(cbrRate[i]);
cbrParams[i].add(jL3Cbr[i]);
cbrParams[i].add(cbrInterPacketInt[i]);
cbrParams[i].add(jL4Cbr[i]);
cbrParams[i].add(cbrMaxPackets[i]);
//Expo Panel
expoParams[i] = new JPanel();
jL1Expo[i] = new JLabel("Packet Size");
expoPacketSize[i] = new JTextField();
jL2Expo[i] = new JLabel("Rate");
expoRate[i] = new JTextField();
jL3Expo[i] = new JLabel("Burst Time");
expoBurstTime[i] = new JTextField();
jL4Expo[i] = new JLabel("Ideal Time");
expoIdealTime[i] = new JTextField();
expoParams[i].add(jL1Expo[i]);
expoParams[i].add(cbrPacketSize[i]);
expoParams[i].add(jL2Expo[i]);
expoParams[i].add(cbrRate[i]);
expoParams[i].add(jL3Expo[i]);
expoParams[i].add(cbrInterPacketInt[i]);
expoParams[i].add(jL4Expo[i]);
expoParams[i].add(cbrMaxPackets[i]);
//FTP Panel
ftpParams[i] = new JPanel();
jL1Ftp[i] = new JLabel("Max Number Packets");
ftpMaxPackets[i] = new JTextField();
jL2Ftp[i] = new JLabel("More Count Packets");
ftpMoreCountPackets[i] = new JTextField();
//create a CardLayout for the options presented
layPanelCard[i] = new CardLayout();
jP2[i].setLayout(layPanelCard[i]);
jP2[i].add("cbrParams",cbrParams[i]);
jP2[i].add("expoParams",expoParams[i]);
jP2[i].add("ftpParams",ftpParams[i]);
layPanelCard[i].show(jP2[i],"cbrParams");
//add it all to the tab
jP[i].add(jL[i]);
jP[i].add(jCb[i]);
jP[i].add(jP2[i]);
//add the tab to the tabbedPane
trafficInfo.addTab(tabName+i,jP[i]);
//trafficInfo.setTitleAt(countTcp,tcpLabel);
}
trafficInfo.updateUI();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ns2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ns2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ns2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ns2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ns2GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
}
答案 0 :(得分:2)
trafficInfo.setTitleAt(countTcp, tcpLabel+i);
看起来countTcp
在某种程度上是不正确的。可能未正确初始化
答案 1 :(得分:0)
固定!我忘了在JPanel数组声明之后初始化JPanel。
//did this
JPanel[] jP2 = new JPanel[noTCPFlows];
//rest of the code
//forgot to add this line
JPanel jP2[i] = new JPanel();
初始化JPanel修复了所有问题。 谢谢你的建议!