Foros del Web » Programación para mayores de 30 ;) » Java »

JTextField.getText() no me captura

Estas en el tema de JTextField.getText() no me captura en el foro de Java en Foros del Web. En falumno.java En esta parte cuando pruebo con: mialumno.alu_nota1 = "3"; mialumno.alu_nota2 = "5"; me da el resultado, todo OK... Pero cuando utilizo el getText(), ...
  #1 (permalink)  
Antiguo 03/03/2011, 01:30
 
Fecha de Ingreso: diciembre-2008
Ubicación: PERU
Mensajes: 294
Antigüedad: 15 años, 4 meses
Puntos: 23
JTextField.getText() no me captura

En falumno.java
En esta parte cuando pruebo con:
mialumno.alu_nota1 = "3";
mialumno.alu_nota2 = "5";

me da el resultado, todo OK...

Pero cuando utilizo el getText(), no me resuelve la operacion
mialumno.alu_nota1 = txt1.getText();
mialumno.alu_nota2 = txt2.getText();




falumno.java
Código Javascript:
Ver original
  1. package src;
  2.  
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JTextField;
  6.  
  7. import src.alumno.Nota;
  8.  
  9.  
  10. public class falumno {
  11.  
  12.     public JFrame falu;
  13.     public JButton btcal;
  14.     public JTextField txt1, txt2, txt3;
  15.  
  16.     public falumno(){
  17.         falu = new JFrame("prueba");
  18.         falu.setSize(400,300);
  19.         falu.setLocation(300,300);
  20.         falu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21.         falu.getContentPane().setLayout(null);
  22.        
  23.        
  24.         btcal = new JButton("Calcula");
  25.         btcal.setSize(90,30);
  26.         btcal.setLocation(150,200);
  27.        
  28.        
  29.         txt1 = new JTextField("");
  30.         txt1.setLocation(150,50);
  31.         txt1.setSize(90,30);
  32.                    
  33.         txt2 = new JTextField("");
  34.         txt2.setLocation(150,90);
  35.         txt2.setSize(90,30);
  36.        
  37.         txt3 = new JTextField();
  38.         txt3.setLocation(150,150);
  39.         txt3.setSize(90,30);
  40.        
  41.         alumno mialumno = new alumno();
  42.         /*mialumno.alu_nota1 = "3";
  43.         mialumno.alu_nota2 = "5";*/
  44.        
  45.         mialumno.alu_nota1 = txt1.getText();
  46.         mialumno.alu_nota2 = txt2.getText();
  47.        
  48.         Nota minota = mialumno.new Nota();
  49.         btcal.addActionListener(minota);
  50.         //txt3.setText(minota.promedio);
  51.        
  52.        
  53.         falu.getContentPane().add(txt1);
  54.         falu.getContentPane().add(txt2);
  55.         falu.getContentPane().add(txt3);
  56.         falu.getContentPane().add(btcal);
  57.         falu.setVisible(true);
  58.     }
  59.    
  60.     public static void main(String[] args){
  61.         falumno ventana = new falumno();               
  62.     }
  63.  
  64. }

alumno.java
Código Javascript:
Ver original
  1. package src;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5.  
  6. public class alumno {
  7.     public String alu_cod;
  8.     public String alu_nomb;
  9.     public String alu_ape;
  10.     public String alu_nota1;
  11.     public String alu_nota2;
  12.     public Integer alu_prom;   
  13.    
  14.  
  15.      
  16.     class Nota implements ActionListener{
  17.         String promedio; {}
  18.            
  19.            
  20.         public void actionPerformed (ActionEvent e){
  21.             try{
  22.                
  23.             alu_prom =  Integer.parseInt(alu_nota1) + Integer.parseInt(alu_nota2);         
  24.             promedio = Integer.toString(alu_prom); 
  25.             System.out.println(promedio);
  26.            
  27.             }catch(Exception x){
  28.                 System.out.println("no hay valores");
  29.  
  30.             }      
  31.         }
  32.     }
  33.            
  34.    
  35. }
  #2 (permalink)  
Antiguo 03/03/2011, 11:58
 
Fecha de Ingreso: diciembre-2008
Ubicación: PERU
Mensajes: 294
Antigüedad: 15 años, 4 meses
Puntos: 23
Respuesta: JTextField.getText() no me captura

alguien q' puede decirme xq no me captura los datos ingresados en :

falumno.java

mialumno.alu_nota1 = txt1.getText();
mialumno.alu_nota2 = txt2.getText();

pero cuando pongo x ejemplo:
mialumno.alu_nota1 = "3";
mialumno.alu_nota2 = "5" ;

si realiza la operacion...
  #3 (permalink)  
Antiguo 03/03/2011, 13:26
Avatar de sivadmp  
Fecha de Ingreso: febrero-2011
Ubicación: La Paz - Bolivia
Mensajes: 293
Antigüedad: 13 años, 1 mes
Puntos: 20
Respuesta: JTextField.getText() no me captura

hola

en primer lugar las clases en Java siempre se declaran con la primera letra en mayuscula, averigua cual es el estándar de codificación en java.

tu clase Nota, no deberia estar dentro de la clase alumno si no dentro de la clase Falumno

en el constructor de la clase Falumno, estas declarando un objeto de tipo alumno, y estas adicionando los valores de las 2 campos de texto, eso siempre te va a dar el valor actual del campo de texto, que en este caso seria ""

Código JAVA:
Ver original
  1. alumno mialumno = new alumno();        
  2.         mialumno.alu_nota1 = txt1.getText();
  3.         mialumno.alu_nota2 = txt2.getText();

otra observacion mas, cuando deseas asignarles valores a un atributo de una clase, siempre tienes que utilizar funciones de getter and setter

entonces tu nueva clase Alumno seria

Código java:
Ver original
  1. public class Alumno {
  2.     public String alu_cod;
  3.     public String alu_nomb;
  4.     public String alu_ape;
  5.     public String alu_nota1;
  6.     public String alu_nota2;
  7.     public Integer alu_prom;
  8.     public String getAlu_cod() {
  9.         return alu_cod;
  10.     }
  11.     public void setAlu_cod(String aluCod) {
  12.         alu_cod = aluCod;
  13.     }
  14.     public String getAlu_nomb() {
  15.         return alu_nomb;
  16.     }
  17.     public void setAlu_nomb(String aluNomb) {
  18.         alu_nomb = aluNomb;
  19.     }
  20.     public String getAlu_ape() {
  21.         return alu_ape;
  22.     }
  23.     public void setAlu_ape(String aluApe) {
  24.         alu_ape = aluApe;
  25.     }
  26.     public String getAlu_nota1() {
  27.         return alu_nota1;
  28.     }
  29.     public void setAlu_nota1(String aluNota1) {
  30.         alu_nota1 = aluNota1;
  31.     }
  32.     public String getAlu_nota2() {
  33.         return alu_nota2;
  34.     }
  35.     public void setAlu_nota2(String aluNota2) {
  36.         alu_nota2 = aluNota2;
  37.     }
  38.     public Integer getAlu_prom() {
  39.         return alu_prom;
  40.     }
  41.     public void setAlu_prom(Integer aluProm) {
  42.         alu_prom = aluProm;
  43.     }    
  44. }


y la clase de tu aplicacion grafica seria

Código java:
Ver original
  1. public class Falumno {
  2.  
  3.     public JFrame falu;
  4.     public JButton btcal;
  5.     public JTextField txt1, txt2, txt3;
  6.  
  7.     public Falumno() {
  8.         falu = new JFrame("prueba");
  9.         falu.setSize(400, 300);
  10.         falu.setLocation(300, 300);
  11.         falu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12.         falu.getContentPane().setLayout(null);
  13.  
  14.         btcal = new JButton("Calcula");
  15.         btcal.setSize(90, 30);
  16.         btcal.setLocation(150, 200);
  17.  
  18.         txt1 = new JTextField();
  19.         txt1.setLocation(150, 50);
  20.         txt1.setSize(90, 30);
  21.  
  22.         txt2 = new JTextField();
  23.         txt2.setLocation(150, 90);
  24.         txt2.setSize(90, 30);
  25.  
  26.         txt3 = new JTextField();
  27.         txt3.setLocation(150, 150);
  28.         txt3.setSize(90, 30);
  29.  
  30.         btcal.addActionListener(new PromedioListener());
  31.         // txt3.setText(minota.promedio);
  32.  
  33.         falu.getContentPane().add(txt1);
  34.         falu.getContentPane().add(txt2);
  35.         falu.getContentPane().add(txt3);
  36.         falu.getContentPane().add(btcal);
  37.         falu.setVisible(true);
  38.     }
  39.  
  40.     public class PromedioListener implements ActionListener {
  41.         String promedio;
  42.    
  43.         public void actionPerformed(ActionEvent e) {
  44.             Alumno alum = new Alumno();
  45.             alum.setAlu_nota1(txt1.getText());
  46.             alum.setAlu_nota2(txt2.getText());
  47.            
  48.             try {
  49.                 alum.alu_prom =
  50.                     Integer.parseInt(alum.getAlu_nota1())+
  51.                     Integer.parseInt(alum.getAlu_nota2());
  52.                 promedio = Integer.toString(alum.alu_prom);
  53.                 txt3.setText(promedio);
  54.             } catch (Exception x) {
  55.                 System.out.println("no hay valores");
  56.  
  57.             }
  58.         }
  59.     }
  60.  
  61.     public static void main(String[] args) {
  62.         Falumno ventana = new Falumno();
  63.     }
  64. }


tienes que notar, que dentro de el evento ActionListener, recién creo una instancia del objeto Alumno.

saludos
  #4 (permalink)  
Antiguo 03/03/2011, 13:56
 
Fecha de Ingreso: diciembre-2008
Ubicación: PERU
Mensajes: 294
Antigüedad: 15 años, 4 meses
Puntos: 23
Respuesta: JTextField.getText() no me captura

@sivadmp muchas gracias ahora si , me funciona.!

Talvez la culpa sea mia xq tengo un pesimo concepto sobre programacion Orientada a objetos (clases con atributos y sus metodos)... pues siempre se dice q una clase tiene atributos y metodos, es por eso que puse esa operacion dentro de Alumno como si fuera un metodo, para asi evitar mostrar las operaciones dentro del codigo del formulario.

-----o-----

Etiquetas: capturar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:14.