我正在使用Netbeans来创建一个jframe,要求提供名字,姓氏,身份证号码,性别和教育水平。我是OOP的新手,特别是Java,所以请耐心地告诉我,请原谅我糟糕的选择。
我首先创建了一个公共类
package registros;
public class estudiantes {
String nombre;
String apellido;
String sexo;
String ci;
String nived;
}
然后我创建了jframe,想法是当我点击按钮" Insertar"(jbutton3(在jButton3ActionPerformed中))它应该获取不同的jtextareas,radiobuttons和checkboxes的值,设置值不同的对象变量,最后将对象放在向量中。我需要帮助的是那个按钮,我不知道如何填写对象"(???)
package registros;
import java.util.*;
public class NewJFrame extends javax.swing.JFrame {
estudiantes es;
Vector v = new Vector (5,1);
String sexoValue;
public NewJFrame() {
this.es = new estudiantes();
initComponents();
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();
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");
jButton3.setText("Insertar");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("Eliminar");
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");
jRadioButton1.getAccessibleContext().setAccessibleName("btnMasc");
jRadioButton2.getAccessibleContext().setAccessibleName("btnFem");
jTextField1.setName(""); // NOI18N
jLabel4.setText("Sexo");
jCheckBox1.setText("Primaria");
jCheckBox2.setText("Secundaria");
jCheckBox3.setText("Universidad");
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt){
String nombreValue = jTextField1.getText();
String apellidoValue = jTextField2.getText();
String ciValue = jTextField3.getText();
if (jRadioButton1.isSelected()){
sexoValue ="Masculino";
}
else
sexoValue = "Femenino";
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
我省略了网格的代码
答案 0 :(得分:1)
在JButton
的动作侦听器中,只需使用getter和setter在必要时在Swing组件中检索和存储信息。例如,对于名为JTextField
的{{1}},您可以通过说textFieldName
来获取文字,也可以通过说String name = textFieldName.getText();
来设置文字字段内的文字。
您可以查看API中可用于swing组件的方法: JComboBox,JTextField等
此外,我认为这是因为您修改了代码,但是您错过了textFieldName.setText("This is my custom string.");
方法的结束括号。
要最终将信息保存回模型类jButton3ActionPerformed
,您应该同样使用getter和setter。例如:
estudiantes
使用getter和setter,您可以轻松地将字符串设置和检索到存储在Swing组件中的值。例如:
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;
}
}
您还应该熟悉Java naming conventions。请注意,我已将Estudiantes information = new Estudiantes();
information.setNombre("12345");
System.out.println(information.getNombre());
Output: 12345
更正为UpperCamelCase,这是类名的约定。
答案 1 :(得分:0)
您需要在estudiantes
类中创建getter and setter method,以便在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);