Java 将值传递给对象变量

Java 将值传递给对象变量,java,swing,variables,Java,Swing,Variables,我使用Netbeans来创建一个jframe,它要求输入名字、姓氏、id号、性别和教育程度。我是OOP新手,特别是Java新手,所以请容忍我,原谅我用词不当 我首先创建了一个公共类 package registros; public class estudiantes { String nombre; String apellido; String sexo; String ci; String nived; } 然后我创建了jf

我使用Netbeans来创建一个jframe,它要求输入名字、姓氏、id号、性别和教育程度。我是OOP新手,特别是Java新手,所以请容忍我,原谅我用词不当

我首先创建了一个公共类

package registros;
   public class estudiantes {
     String nombre;
     String apellido;
     String sexo;
     String ci;
     String nived;
    }
然后我创建了jframe,其思想是当我点击按钮“Insertar”(jbutton3(在jButton3ActionPerformed中))时,它应该获得不同JTextArea、radiobuttons和复选框的值,设置不同对象变量的值,最后将对象放入向量中。我需要的帮助是使用该按钮,我不知道如何“填充对象”(??)

包注册;
导入java.util.*;
公共类NewJFrame扩展了javax.swing.JFrame{
大学生;
向量v=新向量(5,1);
字符串六色值;
公共NewJFrame(){
this.es=新的电子学生();
初始化组件();
私有组件(){
buttonGroup1=newjavax.swing.ButtonGroup();
jLabel1=newjavax.swing.JLabel();
jLabel2=newjavax.swing.JLabel();
jLabel3=newjavax.swing.JLabel();
jLabel5=newjavax.swing.JLabel();
jButton1=newjavax.swing.JButton();
jButton2=newjavax.swing.JButton();
jButton3=newjavax.swing.JButton();
jButton4=newjavax.swing.JButton();
jButton5=newjavax.swing.JButton();
jPanel1=newjavax.swing.JPanel();
JRadioButton 1=newjavax.swing.JRadioButton();
JRadioButton 2=newjavax.swing.JRadioButton();
jTextField1=newjavax.swing.JTextField();
jTextField2=newjavax.swing.JTextField();
jTextField3=newjavax.swing.JTextField();
jLabel4=newjavax.swing.JLabel();
JCheckBox 1=newjavax.swing.JCheckBox();
JCheckBox=newjavax.swing.JCheckBox();
jCheckBox3=newjavax.swing.JCheckBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(“注册学生”);
jLabel1.setText(“Nombre”);
jLabel2.setText(“Apellido”);
jLabel3.setText(“C.I.”);
jLabel5.setText(“指令手册”);
jButton1.setText(“实现”);
jButton2.setText(“客车”);
jButton3.setText(“插入器”);
jButton3.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
JButton3执行的操作(evt);
}
});
jButton4.setText(“Eliminar”);
jButton5.setText(“Salir”);
jButton5.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton5ActionPerformed(evt);
}
});
按钮组1.add(jRadioButton1);
jRadioButton1.setText(“男性”);
按钮组1.add(jRadioButton2);
jRadioButton2.setText(“女性”);
jRadioButton1.getAccessibleContext().setAccessibleName(“btnMasc”);
jRadioButton2.getAccessibleContext().setAccessibleName(“btnFem”);
jTextField1.setName(“”;//NOI18N
jLabel4.setText(“Sexo”);
jCheckBox1.setText(“Primaria”);
jCheckBox2.setText(“Secundaria”);
jCheckBox3.setText(“大学”);
私有void jButton5ActionPerformed(java.awt.event.ActionEvent evt){
系统出口(0);
}                                        
私有void jButton3ActionPerformed(java.awt.event.ActionEvent evt){
字符串nombreValue=jTextField1.getText();
字符串apellidoValue=jTextField2.getText();
字符串ciValue=jTextField3.getText();
if(jRadioButton1.isSelected()){
sexoValue=“阳性”;
}
其他的
sexoValue=“Femenino”;
公共静态void main(字符串参数[]){
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新建NewJFrame().setVisible(true);
}
});
}

我省略了网格的代码

您需要的是在
estudiantes
类中创建一个,以便在
jButton3ActionPerformed
中实例化该类时可以设置该类字段的值

示例:

创建一个getter和setter

 public class estudiantes {
     String nombre;
     String apellido;
     String sexo;
     String ci;
     String nived;

     //set method
     public void setnombre(String nombre)
     {
       this.nombre = nombre;
     }

     //get method
     public String setnombre()
     {
       return this.nombre;
     }

     //do everything
    }
jButton3ActionPerformed
中,实例化类后,可以将值设置为所有值

//above is your code
estudiantes es = new estudiantes();
es.setnombre(nombreValue);

JButton
的操作侦听器中,只需根据需要使用getter和setter在Swing组件中检索和存储信息。例如,对于名为
textfield name
JTextField
或者您可以通过说
textFieldName.setText(“这是我的自定义字符串”);
来设置文本字段内的文本

您可以在API中查看swing组件可用的方法: 等等

另外,我认为这是因为您编辑了代码,但在
jbutton3aactionperformed中缺少一个右括号//above is your code
estudiantes es = new estudiantes();
es.setnombre(nombreValue);
public class Estudiantes {
    private String nombre;
    private String apellido;
    private String sexo;
    private 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;
    }
}
Estudiantes information = new Estudiantes();
information.setNombre("12345");
System.out.println(information.getNombre());

Output: 12345