while循环中的变量JTextField

时间:2013-06-12 10:08:05

标签: java arrays swing jlabel jtextfield

我对此cicle和JTextField存在问题。

我想显示带有变量“g”字段的表单。 然后我想写这个字段并使用输入的数据。 现在我不能cod.gettextfield(),因为字段的名称总是相同的。

我试过使用数组,但程序给了我一个例外:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

我该怎么办? 这是我的计划的一部分。

    while (i <= g) {
        System.out.println(g);
        labels2.add(new JLabel("cod.prodotto " + i));
        JTextField cod = new JTextField(8);
        FocusListener focusListener2 = new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                JTextField campo2 = (JTextField) e.getSource();
                campo2.selectAll();
            }
        };
        fields2.add(cod);
        i++;
    }

2 个答案:

答案 0 :(得分:0)

修改:完整代码示例。如果这不能回答您的问题,请更详细地描述问题。

LinkedList<JTextField> fields2 = new LinkedList<JTextField>();
while (i <= g) {
    System.out.println(g);
    labels2.add(new JLabel("cod.prodotto " + i));
    JTextField cod = new JTextField(8);
    fields2.add(cod);
    FocusListener focusListener2 = new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
            JTextField campo2 = (JTextField) e.getSource();
            campo2.selectAll();
        }
    };
    i++;
}
// save all data
JTextField tf;
for( JTextField tf: fields2 ) {
    database.save( tf.getText() );
}
// save specific field
database.save( fields2.get(5).getText() );

答案 1 :(得分:0)

I want that in the 2nd form I can take and use all the field that I compile, in this case I've problem with take the fields "cod. prodotto" in particular.

我希望你能理解。 我很抱歉,但我不是一个优秀的java,不是很好的英语,所以...:D

package inserimentoDati;
import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;

import javax.swing.*;
import javax.swing.text.html.MinimalHTMLWriter;
public class swing {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
start();

}
});
}
private static void start() {


final JPanel nprodlabel = new JPanel();
nprodlabel.add(new JLabel("numero prodotti acquistati"));
JPanel prodfield = new JPanel();
FocusListener focusListener3 = new FocusAdapter() {
@Override public void focusGained(FocusEvent e) {
JTextField campox = (JTextField)e.getSource();
campox.selectAll();
}
};
final JTextField prod= new JTextField(3);
prodfield.add(prod);

Box groupprod = Box.createHorizontalBox();
groupprod.add(nprodlabel);
groupprod.add(prodfield);
JPanel prodcontainer = new JPanel(new FlowLayout(FlowLayout.LEFT));
prodcontainer.add(groupprod);

JFrame window1 = new JFrame("prodotti");
window1.add(prodcontainer);

JButton vai= new JButton("vai");

vai.setSize(10,10);
vai.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e1) {
    int g=Integer.parseInt(prod.getText());

    final JPanel labels = new JPanel(new GridLayout(10, 1));
    labels.add(new JLabel("Nome"));
    labels.add(new JLabel("Cognome"));
    labels.add(new JLabel("P.Iva"));
    labels.add(new JLabel("Codice Fiscale"));
    labels.add(new JLabel("Via"));
    labels.add(new JLabel("n°"));
    labels.add(new JLabel("Cap"));
    labels.add(new JLabel("Città"));
    labels.add(new JLabel("Provincia"));
    labels.add(new JLabel("mail"));
    JPanel fields = new JPanel(new GridLayout(10, 1));
    FocusListener focusListener = new FocusAdapter() {
    @Override public void focusGained(FocusEvent e) {
    JTextField campo = (JTextField)e.getSource();
    campo.selectAll();
    }
    };
    final JTextField nome= new JTextField(15);
    final JTextField cognome= new JTextField(15);
    final JTextField p_iva= new JTextField(11);
    final JTextField c_f= new JTextField(15);
    final JTextField via= new JTextField(30);
    final JTextField civico=new JTextField(5);
    final JTextField citta= new JTextField(30);
    final JTextField cap= new JTextField(8);
    final JTextField provincia= new JTextField(2);
    final JTextField mail=new JTextField(20);

    fields.add(nome);
    fields.add(cognome);
    fields.add(p_iva);
    fields.add(c_f);
    fields.add(via);
    fields.add(civico);
    fields.add(cap);
    fields.add(citta);
    fields.add(provincia);
    fields.add(mail);



    Box group = Box.createHorizontalBox();
    group.add(labels);
    group.add(fields);



    JPanel container = new JPanel(new FlowLayout(FlowLayout.LEFT));
    container.add(group);

    JFrame window = new JFrame("Form");
    window.add(container);
    JButton invia= new JButton("invia");
    invia.setSize(30,10);
    invia.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    Scriviword nuovofile=new Scriviword("c:/alessandro",nome.getText(),                 cognome.getText(), p_iva.getText(), c_f.getText(), via.getText(), civico.getText(), cap.getText(), citta.getText(), provincia.getText(),mail.getText());
    inserimentoUtente ins=new inserimentoUtente();
    try {
        ins.inserisciUtente(nome.getText(), cognome.getText(), p_iva.getText(), c_f.getText(), via.getText(), civico.getText(), cap.getText(), citta.getText(), provincia.getText(),mail.getText());
    } catch (Exception e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    try {
        nuovofile.creaDoc();
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    }});
    JPanel panel= new JPanel();
    panel.add(invia);

    window.add(panel,BorderLayout.SOUTH);
    window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    window.pack();
    window.setVisible(true);


    final JPanel labels2 = new JPanel(new GridLayout(g, 1));
    JPanel fields2 = new JPanel(new GridLayout(g, 1));
    int i=1;

    //JTextField[] cod=new JTextField[g];
    while (i<=g){
        System.out.println(g);
        labels2.add(new JLabel("cod.prodotto "+i));

        //cod[i]= new JTextField(8);
        JTextField cod=new JTextField(8);
        FocusListener focusListener2 = new FocusAdapter() {
            @Override public void focusGained(FocusEvent e) {
            JTextField campo2 = (JTextField)e.getSource();
            campo2.selectAll();
            }
            };

            fields2.add(cod);

        i++;

    }





        Box group = Box.createHorizontalBox();
        group.add(labels);
        group.add(fields);
        Box group2 = Box.createHorizontalBox();
        group2.add(labels2);
        group2.add(fields2);



        JPanel container = new JPanel(new FlowLayout(FlowLayout.LEFT));

        container.add(group);
        container.add(group2);

        JFrame window = new JFrame("Form");
        window.add(container);

        JButton invia= new JButton("invia");
        invia.setSize(30,10);
        invia.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        Scriviword nuovofile=new Scriviword("c:/alessandro",nome.getText(), cognome.getText(), p_iva.getText(), c_f.getText(), via.getText(), civico.getText(), cap.getText(), citta.getText(), provincia.getText(),mail.getText());
        inserimentoUtente ins=new inserimentoUtente();
        try {

            ins.inserisciUtente(nome.getText(), cognome.getText(), p_iva.getText(), c_f.getText(), via.getText(), civico.getText(), cap.getText(), citta.getText(), provincia.getText(),mail.getText());
        } catch (Exception e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        try {
            nuovofile.creaDoc();
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        }});
        JPanel panel= new JPanel();
        panel.add(invia);

        window.add(panel,BorderLayout.SOUTH);
        window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        window.pack();
        window.setVisible(true);
        /*riportata qui */


}
});






JPanel pannellino=new JPanel();

pannellino.add(vai);
window1.add(pannellino,BorderLayout.SOUTH);
window1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
window1.pack();
window1.setVisible(true);

}}