Programación General > Java

 EVENTOS DE BOTONES

(1/1)

andres69:
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 ---import javax.swing.*;import java.awt.*;import java.awt.event.*;public class eliminar {    JButton boton1,boton2;    JLabel etiqueta;    JFrame marco;        eliminar()    {        JFrame.setDefaultLookAndFeelDecorated(true);        JFrame marco=new JFrame("Ejemplo de eventos de dos botones");        marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        boton1=new JButton("boton uno");        boton1.addActionListener(new sumaDos());        boton2=new JButton("boton dos");        boton2.addActionListener(new sumaCuatro());        etiqueta=new JLabel("Sin Pulsar");        JPanel panel=new JPanel(new GridLayout(0,1));        panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));        panel.add(boton1);        panel.add(boton2);        panel.add(etiqueta);        marco.add(panel, BorderLayout.CENTER);        marco.pack();        marco.setVisible(true);    }        class sumaDos    {        public void actionPerformed(ActionEvent e)        {            etiqueta.setText("Preciono el boton uno");        }    }        class sumaCuatro    {        public void actionPerformed(ActionEvent e)        {            etiqueta.setText("Preciono el boton dos");        }    }                public static void main(String[] args)    {        eliminar w=new eliminar();    }} [/color]
ME FUI GIANDO TAMBIEN EN EL SIGUIENTE EJEMPPLO:



--- Código: Text ---import javax.swing.*;import java.awt.*;import java.awt.event.*;public class PrimerEventoVentana {    JTextArea areaTexto;        PrimerEventoVentana()     {        JFrame.setDefaultLookAndFeelDecorated(true);        JFrame marco=new JFrame("Eventos de ventana");        marco.addWindowListener(new GestionaVentana());        areaTexto=new JTextArea();        marco.add(areaTexto);        marco.setSize(300,200);        marco.setVisible(true);    }        public static void main(String[] args)     {        PrimerEventoVentana e1=new PrimerEventoVentana();    }        class GestionaVentana extends WindowAdapter    {        public void windowClosing(WindowEvent e)        {            areaTexto.append("Cerrando la ventanan");            System.exit(0);        }                public void windowOpened(WindowEvent e)        {            areaTexto.append("Abierta la ventanan");        }                public void windowIconified(WindowEvent e)        {            areaTexto.append("Minimizada la ventanan");        }                public void windowDeiconified(WindowEvent e)        {            areaTexto.append("Desplegando la ventanan");        }                public void windowDeactivated(WindowEvent e)        {            areaTexto.append("Desactivada la ventanan");        }    }} 
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:
Las clases sumaDos y sumaCuatro no son ActionListener; prueba extendiendo esa clase:

--- Código: Java ---class sumaDos extends ActionListener {//...} 
:suerte:

andres69:

--- Cita de: "shadow_rev" ---Las clases sumaDos y sumaCuatro no son ActionListener; prueba extendiendo esa clase:

--- Código: Java ---class sumaDos extends ActionListener {//...} 
:suerte:
--- Fin de la cita ---

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

Mil gracias!!

 :good:  :beer:

Navegación

[0] Índice de Mensajes

Ir a la versión completa