3

嘿,我在这里有点新,所以如果我在帖子中搞砸了,我深表歉意。无论如何,我必须处理数组的问题,我要做的基本上是使用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("");
    }
}

4

1 回答 1

4
public String[] getForm()
{
    formReturn = formArray; /* (0) */
    formReturn[0] = formArray[0];
    for(int i = 1; i < formReturn.length; i++)
        formReturn[i] = text[i].getText(); /* (1) */
    return formReturn;
}

查看第 (1) 行:您修改了指向标签文本的 formReturn 数组。formReturn -> formArray -> com.

要修复它,只需在 (0) 处创建新的字符串数组:

formReturn = new String[formArray.length];
于 2012-04-29T06:04:55.707 回答