嗨,我是一个新手,如果我的帖子中有什么问题,请多包涵。无论如何,我遇到的问题与数组有关,基本上我想做的是使用String[]数组填充表格并在屏幕上显示它,然后使 getForm()函数返回一个 String[] ,其中包括表单标题和 text [i] 中的信息。所有这些都很好地运行,直到我使用添加到调用 getForm()函数的按钮并更改为不同的表单(附加到ListListener的 createForm() )时,所有标签都显示为以前在 getForm()函数中返回的内容。我相当确定它与我使用数组的方式有关,但我认为选择另一个列表项后,它们将重新设置为正常状态,这将再次通过 createForm()函数重置数组,所以我不确定出了什么问题。
谢谢
我还附上了下面的截图。
http://www.majhost.com/gallery/adc90/afsd/error.png
class Form extends JPanel
{
//Arrays for the forms
private String[] com = {"Communication","ICAO","Type","Frequency"};
private String[] fuel = {"Fuel","ICAO","Type"};
private String[] runway = {"Runway","ICAO","Number","Type","Length"};
private String[] airplane = {"Airplane","Make","Model","Type","Fuel Capacity", "Fuel Burn Rate", "Air Speed"};
private String[] airport = {"Airplane","ICAO","Name","Longitude","Latitude","crFreq","crType", "Fuel Type"};
//Declare variables
private JTextField[] text;
private String[] formReturn;
private String[] formArray;
private JButton submit,clear;
public Form()
{
createForm("Airplane");
}
public void createForm(String choice)
{
removeAll();
if(choice.equals("Communication"))
{
formArray = com;
}
else if(choice.equals("Fuel"))
{
formArray = fuel;
}
else if(choice.equals("Airplane"))
{
formArray = airplane;
}
else if(choice.equals("Airport"))
{
formArray = airport;
}
else if(choice.equals("Runway"))
{
formArray = runway;
}
int l = formArray.length + 1;
text = new JTextField[l];
//Layout info
GridLayout grid = new GridLayout(l,2);
grid.setHgap(0);
setLayout(grid);
//Set label
add(new JLabel(formArray[0]));
add(new JLabel(""));
for(int i = 1; i < formArray.length; ++i)
{
add(new JLabel(formArray[i]));
add(text[i] = new JTextField(20));
}
//Add in the buttons and the actionlisteners
submit = new JButton("Create");
clear = new JButton("Delete");
add(clear);
clear.addActionListener(new Button());
add(submit);
submit.addActionListener(new Button());
updateUI();
}
//Get form info
//This works so far
public String[] getForm()
{
formReturn = formArray;
formReturn[0] = formArray[0];
for(int i = 1; i < formReturn.length; i++)
formReturn[i] = text[i].getText();
return formReturn;
}
//Clear form
public void clearForm()
{
for(int i = 1; i < formArray.length; i++)
text[i].setText("");
}
}