Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/03/2011, 14:14
yuo2
 
Fecha de Ingreso: diciembre-2008
Ubicación: PERU
Mensajes: 294
Antigüedad: 15 años, 4 meses
Puntos: 23
ejecutar un metodo en java..?

Hola alguien sabe como llamar un evento y ejecutarlo desde un boton?
Estoy tratando de hacer algo tan simple como:
tengo mi clase alumno con sus atributos y un metodo llamado Nota, ahora ese evento lo llamo en mi falumno q vendria hacer mi formulario.

Bueno nose si esta bien lo q hago, a ver si alguien me corrije..


falumno (formulario alumno) aqui hago mi interface en swing.. la cual quiero ejecutar en un boton el metodo Nota (de la clase alumnos) q esta en otro archivo java

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


Clase Alumno tiene algunos atributos y un metodo Nota.

alumno.java
Código Javascript:
Ver original
  1. package src;
  2.  
  3. import java.awt.event.*;
  4.  
  5. public class alumno {
  6.     public String alu_cod;
  7.     public String alu_nomb;
  8.     public String alu_ape;
  9.     public String alu_nota1;
  10.     public String alu_nota2;
  11.     public Integer alu_prom;
  12.    
  13.    
  14.     class Nota implements ActionListener{
  15.         public void actionPerformed (ActionEvent evento){
  16.             alu_prom = Integer.parseInt(alu_nota1)+ Integer.parseInt(alu_nota2);
  17.             System.out.println(alu_prom);
  18.         }  
  19.     }
  20.    
  21. }