• Domingo 5 de Mayo de 2024, 13:26

Autor Tema:  EVENTOS DE BOTONES  (Leído 2605 veces)

andres69

  • Miembro MUY activo
  • ***
  • Mensajes: 117
  • Nacionalidad: mx
    • Ver Perfil
    • http://alldownload.foroes.net
EVENTOS DE BOTONES
« en: Domingo 21 de Diciembre de 2008, 06:52 »
0
HOLA, APENAS EMPIEZO A PROGRAMAR EN JAVA EN LO GRAFICO. ESTOY INTENTADO AGREGAR DOS BOTONES, SI PRECIONO EL BOTON 1 SALDRA EL TEXTO "PRECIONO EL BOTON 1" Y SI PRECIONO EL BOTON 2 DIRA "PRECIONO EL BOTON 2" ESTOS TEXTO LO VA A MOSTRAR EN UNA ETIQUETA, PERO ME SALEN DOS ERRORES, ESTE ES EL CODIGO:

Código: Text
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class eliminar
  5. {
  6.     JButton boton1,boton2;
  7.     JLabel etiqueta;
  8.     JFrame marco;
  9.    
  10.     eliminar()
  11.     {
  12.         JFrame.setDefaultLookAndFeelDecorated(true);
  13.         JFrame marco=new JFrame("Ejemplo de eventos de dos botones");
  14.         marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.         boton1=new JButton("boton uno");
  16.         boton1.addActionListener(new sumaDos());
  17.         boton2=new JButton("boton dos");
  18.         boton2.addActionListener(new sumaCuatro());
  19.         etiqueta=new JLabel("Sin Pulsar");
  20.         JPanel panel=new JPanel(new GridLayout(0,1));
  21.         panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
  22.         panel.add(boton1);
  23.         panel.add(boton2);
  24.         panel.add(etiqueta);
  25.         marco.add(panel, BorderLayout.CENTER);
  26.         marco.pack();
  27.         marco.setVisible(true);
  28.     }
  29.    
  30.     class sumaDos
  31.     {
  32.         public void actionPerformed(ActionEvent e)
  33.         {
  34.             etiqueta.setText("Preciono el boton uno");
  35.         }
  36.     }
  37.    
  38.     class sumaCuatro
  39.     {
  40.         public void actionPerformed(ActionEvent e)
  41.         {
  42.             etiqueta.setText("Preciono el boton dos");
  43.         }
  44.     }
  45.        
  46.    
  47.     public static void main(String[] args)
  48.     {
  49.         eliminar w=new eliminar();
  50.     }
  51. }
  52.  
[/color]
ME FUI GIANDO TAMBIEN EN EL SIGUIENTE EJEMPPLO:


Código: Text
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class PrimerEventoVentana
  5. {
  6.     JTextArea areaTexto;
  7.    
  8.     PrimerEventoVentana()
  9.     {
  10.         JFrame.setDefaultLookAndFeelDecorated(true);
  11.         JFrame marco=new JFrame("Eventos de ventana");
  12.         marco.addWindowListener(new GestionaVentana());
  13.         areaTexto=new JTextArea();
  14.         marco.add(areaTexto);
  15.         marco.setSize(300,200);
  16.         marco.setVisible(true);
  17.     }
  18.    
  19.     public static void main(String[] args)
  20.     {
  21.         PrimerEventoVentana e1=new PrimerEventoVentana();
  22.     }
  23.    
  24.     class GestionaVentana extends WindowAdapter
  25.     {
  26.         public void windowClosing(WindowEvent e)
  27.         {
  28.             areaTexto.append("Cerrando la ventanan");
  29.             System.exit(0);
  30.         }
  31.        
  32.         public void windowOpened(WindowEvent e)
  33.         {
  34.             areaTexto.append("Abierta la ventanan");
  35.         }
  36.        
  37.         public void windowIconified(WindowEvent e)
  38.         {
  39.             areaTexto.append("Minimizada la ventanan");
  40.         }
  41.        
  42.         public void windowDeiconified(WindowEvent e)
  43.         {
  44.             areaTexto.append("Desplegando la ventanan");
  45.         }
  46.        
  47.         public void windowDeactivated(WindowEvent e)
  48.         {
  49.             areaTexto.append("Desactivada la ventanan");
  50.         }
  51.     }
  52. }
  53.  

YA LO REVISE Y NO ME HACE FALTA ALGUNA LETRA O SIMBOLO AL PARECER ESTA BIEN SEGUN YO, PERO REPITO APENAS ESTOY EMPEZANDO, LOS ERRORES QUE ME SALE SON:
addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (eliminar.sumaCuatro)
addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (eliminar.sumaDos)

ESPERO QUE ME PUEDAN AYUDAR

GRACIAS!!!!

shadow_rev

  • Miembro MUY activo
  • ***
  • Mensajes: 397
  • Nacionalidad: co
  • Un SPARTAN no muere en combate
    • Ver Perfil
    • http://shadowrev.blogspot.com
Re: EVENTOS DE BOTONES
« Respuesta #1 en: Domingo 21 de Diciembre de 2008, 22:35 »
0
Las clases sumaDos y sumaCuatro no son ActionListener; prueba extendiendo esa clase:
Código: Java
  1. class sumaDos extends ActionListener {
  2. //...
  3. }
  4.  

:suerte:
Volará quien le ponga alas a sus sueños (Candidate for goddess)
Si el mal existe en este mundo, reside en el corazón de la humanidad (Edward D. Morrison - Tales of Phantasia)
Lo único que puedes cambiar del pasado, es lo que sientes por él en el presente (Lockon Stratos - Mobile Suit Gundam 00)
Ingeniero de Sistemas

andres69

  • Miembro MUY activo
  • ***
  • Mensajes: 117
  • Nacionalidad: mx
    • Ver Perfil
    • http://alldownload.foroes.net
Re: EVENTOS DE BOTONES
« Respuesta #2 en: Martes 23 de Diciembre de 2008, 01:50 »
0
Cita de: "shadow_rev"
Las clases sumaDos y sumaCuatro no son ActionListener; prueba extendiendo esa clase:
Código: Java
  1. class sumaDos extends ActionListener {
  2. //...
  3. }
  4.  

:suerte:

gracias shadown_rev funciono!!!!!!! solo hacia fata eso.

Mil gracias!!

 :good:  :beer: