使用Java将项添加到列表中

时间:2014-06-09 20:24:24

标签: java swing list

我必须监督一些非常简单的事情。我只是不能在列表中添加多个对象。我使用的是使用Netbeans制作的jFrame。 首先是我的对象类

package registros;

/**
 *
 * @author juan
 */
public class estudiantes {
 private String nombre;
 private String apellido;
 private String sexo;
 public String ci;
 private String nived;

public String getNombre() {
    return nombre;
}
public void setNombre(String nombre) {
    this.nombre = nombre;
}
public String getApellido() {
    return apellido;
}
public void setApellido(String apellido) {
    this.apellido = apellido;
}
public String getSexo() {
    return sexo;
}
public void setSexo(String sexo) {
    this.sexo = sexo;
}
public String getCi() {
    return ci;
}
public void setCi(String ci) {
    this.ci = ci;
}
public String getNived() {
    return nived;
}
public void setNived(String nived) {
    this.nived = nived;
}

}

我把ci公之于众,因为当我试图访问这些值时,我收到一条错误消息,说它被设置为私有。

当我点击按钮3时,它应该读取jTextFields,dadio按钮和复选框的值(我在这个论坛的帮助下工作了,谢谢你们!)但是我的列表只存储了最后一组它读取的价值。我究竟做错了什么?我编辑了帖子,以显示我认为与代码最相关的内容。

package registros;

import java.awt.Color;
import java.util.*;


public class NewJFrame extends javax.swing.JFrame {
  // List <EST> Arreglo = new ArrayList()<EST>;
  List <estudiantes>registro;
  estudiantes es;
int a=0;
//Vector v = new Vector (5,1);
String sexoValue, nivedValue;

public NewJFrame() {
    this.registro = new ArrayList();
    this.es = new estudiantes();
    initComponents();
}

@SuppressWarnings("unchecked")

private void initComponents() {

    buttonGroup1 = new javax.swing.ButtonGroup();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();
    jPanel1 = new javax.swing.JPanel();
    jRadioButton1 = new javax.swing.JRadioButton();
    jRadioButton2 = new javax.swing.JRadioButton();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    jCheckBox1 = new javax.swing.JCheckBox();
    jCheckBox2 = new javax.swing.JCheckBox();
    jCheckBox3 = new javax.swing.JCheckBox();
    jLabel6 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    jLabel8 = new javax.swing.JLabel();
    jLabel9 = new javax.swing.JLabel();
    jLabel10 = new javax.swing.JLabel();
    jButton6 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Registro Estudiantes");

    jLabel1.setText("Nombre");

    jLabel2.setText("Apellido");

    jLabel3.setText("C.I.");

    jLabel5.setText("Nivel de Instruccion");

    jButton1.setText("Actualizar");

    jButton2.setText("Buscar");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    jButton3.setText("Insertar");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });

    jButton4.setText("Eliminar");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });

    jButton5.setText("Salir");
    jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
        }
    });

    buttonGroup1.add(jRadioButton1);
    jRadioButton1.setText("Masculino");

    buttonGroup1.add(jRadioButton2);
    jRadioButton2.setText("Femenino");


    jTextField1.setName(""); 

    jLabel4.setText("Sexo");

    jCheckBox1.setText("Primaria");

    jCheckBox2.setText("Secundaria");

    jCheckBox3.setText("Universidad");





private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String nombreValue = jTextField1.getText();
        String apellidoValue = jTextField2.getText();
        String ciValue = jTextField3.getText();


        String nivedValue;
        if (jRadioButton1.isSelected()){
            sexoValue ="Masculino";
        }
        else
            sexoValue = "Femenino";
        if (jCheckBox2.isSelected())
            a++;
        if (jCheckBox3.isSelected())
            a= a+2;

        if (a==0)
            nivedValue ="Primaria";
        if (a==1)
            nivedValue = "Secundaria";
            else
            nivedValue = "Universitaria";

        es.setNombre(nombreValue); 
        es.setApellido(apellidoValue);
        es.setSexo(sexoValue);
        es.setNived(nivedValue);
        es.setCi(ciValue);
        registro.add(es);
        jTextField1.setText(null);
        jTextField2.setText(null);
        jTextField3.setText(null);
        jCheckBox1.setSelected(false);
        jCheckBox2.setSelected(false);
        jCheckBox3.setSelected(false);
        jRadioButton1.setSelected(false);
        jRadioButton2.setSelected(false);
    // TODO add your handling code here:
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    try{
      for(estudiantes est: registro){
          if (est.ci.equals(jTextField3.getText())){
    //String searchKey = jTextField3.getText();    
    //if (v.contains (searchKey)){


    jLabel6.setText(es.getNombre());
    jLabel7.setText(es.getApellido());
    jLabel8.setText(es.getCi());
    jLabel9.setText(es.getSexo());
    jLabel10.setText(es.getNived());
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
    jCheckBox1.setSelected(false);
    jCheckBox2.setSelected(false);
    jCheckBox3.setSelected(false);
    jRadioButton1.setSelected(false);
    jRadioButton2.setSelected(false);
          }}
}                                        
catch (NullPointerException e){

jTextField3.setBackground(Color.red);};

}



public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);

        }
    });
}

1 个答案:

答案 0 :(得分:1)

estudiantes es;是一个全局变量。并在构造函数中初始化它。然后在插入按钮上单击,将其插入列表..但在随后的插入点击中,您只需修改相同的元素并再次添加到列表中。因此,列表将多次添加相同的元素。全部都有最后的价值。

要在jButton3ActionPerformed()方法中添加es = new estudiantes();之前修复此问题,然后再将值设置为es