• Sábado 27 de Abril de 2024, 17:54

Autor Tema:  Ingresar Datos por ventana  (Leído 2982 veces)

franZn

  • Nuevo Miembro
  • *
  • Mensajes: 8
    • Ver Perfil
Ingresar Datos por ventana
« en: Viernes 30 de Marzo de 2012, 04:15 »
0
Buenas gente, les comento que estoy haciendo un programa medio estupido, pero a fines de entender como funcionan las ventanas me sirve.
el programa trata de:
en 2 JTextFields se ingresan numeros y usando JMenu con diferentes items (sumar, restar, etc..), luego el resultado se muestra en un 3et textfield.

Eso funciona correctamente.

Ahora lo que pretendo hacer es ingresar Un legajo por un Jtextfield y LUEGO  una nota (int), ingresar dichos datos en un arreglo de objetos alumno, todo a traves de la ventana (no por consola) y con un boton aceptar. pero me he quedado sin herramientas para seguir con mi programa.
Les adjunto el codigo a ver como me pueden ayudar.

Muchas Gracias!!

Código: Java(TM) 2 Platform Standard Edition 5.0
  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. // BARRAS, MENUS, OPERACIONES ARITMETICAS, SE USA SWITCH, SE SUMAN JTextFields//
  5. import javax.swing.*;
  6. public class Menu extends JFrame implements ActionListener {
  7.  
  8.         private JMenuBar barra;
  9.         private JMenu operaciones,edicion;
  10.         private JMenuItem sumar,restar,multiplicar,dividir,carga;
  11.         private JTextField num1,num2,resultado,nombres;
  12.         private JButton boton1;
  13.         private JLabel label;
  14.         private alumnos[] alu = new alumnos[2];
  15.        
  16.        
  17.         public Menu ()
  18.         {
  19.                
  20.                 super ("Operaciones");
  21.        
  22.                 this.setLayout(new FlowLayout());
  23.                 this.setSize(300,300);
  24.                 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  25.                 this.setVentana();
  26.                 this.setJMenuBar(barra);
  27.  
  28.                 this.setVisible(true);
  29.                 this.sumar.addActionListener(this);
  30.                 this.restar.addActionListener(this);
  31.                 this.multiplicar.addActionListener(this);
  32.                 this.carga.addActionListener(this);
  33.                 this.boton1.addActionListener(this);
  34.         }
  35.        
  36.        
  37.        
  38.        
  39.         public void setVentana()
  40.         {
  41.                
  42.                 barra = new JMenuBar();
  43.                 operaciones = new JMenu ("Operaciones");
  44.                 edicion = new JMenu ("Edicion");
  45.                 sumar = new JMenuItem ("Sumar");
  46.                 restar = new JMenuItem ("Restar");
  47.                 multiplicar = new JMenuItem ("Multiplicar");
  48.                 dividir = new JMenuItem ("Dividir");
  49.                 num1 = new JTextField(2);
  50.                 num2 = new JTextField(2);
  51.                 resultado = new JTextField(2);
  52.                 nombres = new JTextField(20);
  53.                 boton1 = new JButton("Aceptar");
  54.                 carga = new JMenuItem("Modo Carga");
  55.                
  56.                
  57.                
  58.                 barra.add(operaciones);
  59.                 barra.add(edicion);
  60.                 operaciones.add(sumar);
  61.                 operaciones.add(restar);
  62.                 operaciones.add(multiplicar);
  63.                 operaciones.add(dividir);
  64.                 edicion.add(carga);
  65.                
  66.                 this.add(num1);
  67.                 this.add(num2);
  68.                 this.add(resultado);
  69.                 this.add(nombres);
  70.                 this.add(boton1);
  71.                 boton1.setVisible(false);
  72.                
  73.         }
  74.                
  75.                
  76.        
  77.         public void actionPerformed(ActionEvent e) {
  78.                 int s=0;
  79.                 if (e.getSource() == sumar)
  80.                 s=0;
  81.                 else if (e.getSource() == restar)
  82.                         s=1;
  83.                 else if (e.getSource() == multiplicar)
  84.                         s=2;
  85.                 else
  86.                        
  87.                         s=3;
  88.                
  89.                        
  90.                
  91.                 switch (s){
  92.                
  93.                 case 0: setSuma();
  94.                 break;
  95.                
  96.                 case 1: setResta();
  97.                 break;
  98.                
  99.                 case 2: setMulti();
  100.                 break;
  101.                
  102.                 case 3:{
  103.                         setCarga();
  104.                         boton1.setVisible(true);
  105.                         num1.setVisible(false);
  106.                         num2.setVisible(false);
  107.                         resultado.setVisible(false);
  108.                        
  109.                 }
  110.                        
  111.                
  112.                
  113.                 }
  114.                
  115.                        
  116.                        
  117.                                
  118.                
  119.         }
  120.  
  121.        
  122.         public void setSuma()
  123.         {
  124.                 int a,b=0;
  125.                 String c;
  126.                
  127.                 a = Integer.parseInt(num1.getText());
  128.                 b = Integer.parseInt(num2.getText());
  129.                 c= Integer.toString(a+b);
  130.                 resultado.setText(c);
  131.                
  132.                
  133.         }
  134.        
  135.         public void setResta()
  136.         {
  137.                 int a,b=0;
  138.                 String c;
  139.                
  140.                 a = Integer.parseInt(num1.getText());
  141.                 b = Integer.parseInt(num2.getText());
  142.                 c= Integer.toString(a-b);
  143.                 resultado.setText(c);
  144.                
  145.                
  146.         }
  147.        
  148.        
  149.         public void setMulti()
  150.         {
  151.                 int a,b=0;
  152.                 String c;
  153.                
  154.                 a = Integer.parseInt(num1.getText());
  155.                 b = Integer.parseInt(num2.getText());
  156.                 c= Integer.toString(a*b);
  157.                 resultado.setText(c);
  158.                
  159.                
  160.         }
  161.        
  162.        
  163.         public void setCarga()
  164.         {
  165.                 int a,b,i=0;
  166.                 label = new JLabel("Ingrese Legajos y Notas");
  167.                 this.add(label);
  168.                 alu = new alumnos[2];
  169.                 // Y AHORAAAA??? :(
  170.                
  171.                
  172.                
  173.         }
  174. }
  175.  
  176.  
  177.  
  178. public class alumnos {
  179.  
  180.         private int legajo=0;
  181.         private int nota=0;
  182.        
  183.        
  184.  
  185.  
  186. public void setNotas (int leg, int nota)
  187. {
  188.         legajo= leg;
  189.         this.nota = nota;
  190.        
  191.        
  192.        
  193. }
  194.  
  195.  
  196.  
  197. }
  198.  
  199.  
  200. public class Main {
  201.  
  202.  
  203.         public static void main(String[] args) {
  204.        
  205.                 Menu me = new Menu();
  206.  
  207.         }
  208.  
  209. }
  210.  
  211.  
  212.  

arielb

  • Moderador
  • ******
  • Mensajes: 771
  • Nacionalidad: pa
    • Ver Perfil
    • http://coder-pa.blogspot.com
Re:Ingresar Datos por ventana
« Respuesta #1 en: Viernes 30 de Marzo de 2012, 16:52 »
0
pero me he quedado sin herramientas para seguir con mi programa.
"Porque de tal manera amó Dios al mundo que dio a su hijo unigénito para que todo aquél que en él crea no se pierda mas tenga vida eterna"
Juan 3:16

http://coder-pa.blogspot.com

franZn

  • Nuevo Miembro
  • *
  • Mensajes: 8
    • Ver Perfil
Re:Ingresar Datos por ventana
« Respuesta #2 en: Viernes 30 de Marzo de 2012, 17:31 »
0
pero me he quedado sin herramientas para seguir con mi programa.

no entiendo para que citas esto..

Me he quedado sin herramientas me refiero a que no se como seguir con mi codigo.

Gracias

arielb

  • Moderador
  • ******
  • Mensajes: 771
  • Nacionalidad: pa
    • Ver Perfil
    • http://coder-pa.blogspot.com
Re:Ingresar Datos por ventana
« Respuesta #3 en: Lunes 2 de Abril de 2012, 16:41 »
0
Hola, disculpa la verdad me parece que había puesto un comentario, aparentemente no fue así.
Si, lo que quería preguntar era eso, que querías decir exactamente con eso.
Bueno la idea no es entrar en discusión.

En tú método actionperformed puedes agregar algo así:

Código: Java(TM) 2 Platform Standard Edition 5.0
  1. //Esto hará que cuando presiones el botón que tiene Aceptar haga algo que le pongas con código.
  2. if ( e.getActionCommand().equals("Aceptar")){
  3.       //num1.getText().toString()......
  4.           //todo código.
  5. }

Saludos,
"Porque de tal manera amó Dios al mundo que dio a su hijo unigénito para que todo aquél que en él crea no se pierda mas tenga vida eterna"
Juan 3:16

http://coder-pa.blogspot.com