Java - Como utilizar varios Action Listener y ItemListener en el mismo JFrame

 
Vista:
sin imagen de perfil

Como utilizar varios Action Listener y ItemListener en el mismo JFrame

Publicado por Daniè (1 intervención) el 18/11/2013 02:06:08
Buen dia colegas,tenia esta duda.

Estoy ya dentro del tema de visual en java,ahora me piden hacer un programa que de un JCombobox despliegue una lista de películas predefinidas y llene los campos genero,duración y clasificación que son JTextFields hasta ahí tengo todo bien.

Lo he hecho con el ItemListener.

Pero pero tengo 2 botones uno llamado limpiar y otro salir,ya se como programarlos,el problema es que no se como implementarlo porque necesito usar ActionListener para los botones.

Como puedo usar ambos escuchadores en el Frame el problema viene cuando hago el frame a mi me enseñaron a declararlo así

class Peliculas extends JFrame implements ItemListener{
.
.
.

public static void main(String[] args) {

Peliculas P=new Peliculas();

}
}

Realmente solo necesito saber como puedo hacer para que pueda utilizar el ActionListener porque la programación de los botones seria así:

public void actionPerformed(ActionEvent evt)
{
Object presionado=evt.getSource();
if(presionado==cmdlimpiar)
limpiar();
if(presionado==cmdsalir)
System.exit(0);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
class Peliculas extends JFrame implements ItemListener {
 
		//Icono
		ImageIcon icono = new ImageIcon("1.JPEG");
		JLabel imagen = new JLabel();
		//Labels
		JLabel lbltitulo=new JLabel("CATALOGO DE PELICULAS: ");
		JLabel lblgenero=new JLabel("GENERO: ");
		JLabel lblduracion=new JLabel("DURACION: ");
		JLabel lblclasificacion=new JLabel("CLASIFICACION: ");
		JLabel lblformato=new JLabel("FORMATO");
		//JTextFields
		JTextField jtxtgenero=new JTextField();
		JTextField jtxtduracion=new JTextField();
		JTextField jtxtclasificacion=new JTextField();
 
		//Combobox
		String[] code = { "Batman The Dark Knight Rises", "Transformers The Dark Side Of The Moon", "Fast & Furious 6" };
		JComboBox combo=new JComboBox(code);
 
		//Checkbox
		JCheckBox chkopcion2D = new JCheckBox("2D",false);
 		JCheckBox chkopcion3D = new JCheckBox("3D",false);
 		JCheckBox chkopcion4D = new JCheckBox("4D",false);
 		JCheckBox chkopcionIMAX = new JCheckBox("IMAX",false);
 
		//JButtons
		JButton cmdlimpiar=new JButton("LIMPIAR");
		JButton cmdsalir=new JButton("SALIR");
 
		//Constructor
		public Peliculas(){
 
		super("CATALOGO DE PELICULAS");
		this.setBounds(0,0,800,600);
		this.setLocationRelativeTo(null);
 
		//COMBOBOX
		combo.setBounds(40,200,180,19);
		//Labels Posicion y Tamaño
		lbltitulo.setBounds(300,160,180,19);
		lblgenero.setBounds(300,200,180,19);
		lblduracion.setBounds(300,220,180,19);
		lblclasificacion.setBounds(300,240,180,19);
		lblformato.setBounds(350,270,189,19);
		//Labels Posicion y Tamaño
		jtxtgenero.setBounds(400,200,180,19);
		jtxtduracion.setBounds(400,220,180,19);
		jtxtclasificacion.setBounds(400,240,180,19);
 
		//Chechkbox
		chkopcion2D.setBounds(300,300,180,19);
		chkopcion3D.setBounds(300,320,180,19);
		chkopcion4D.setBounds(300,340,180,19);
		chkopcionIMAX.setBounds(400,300,300,25);
 
		//Labels Posicion y Tamaño
		cmdlimpiar.setBounds(300,400,90,19);
		cmdsalir.setBounds(400,400,90,19);
 
		//Imagen
		imagen.setBounds(100,0,600,145);
 
		setLayout(null);
		ImageIcon icono2 = new ImageIcon(icono.getImage().getScaledInstance(1, 11,Image.SCALE_DEFAULT));
		imagen.setIcon(icono);
		this.add(imagen);
 
		this.add(lbltitulo);
		this.add(lblgenero);
		this.add(lblduracion);
		this.add(lblclasificacion);
		this.add(lblformato);
		this.add(jtxtgenero);
		this.add(jtxtduracion);
		this.add(jtxtclasificacion);
 
		add(combo);/*
		combo.addItem("Batman The Dark Knight Rises");
		combo.addItem("Transformers The Dark Side Of The Moon");
		combo.addItem("Fast & Furious 6");
		combo.addActionListener(this);*/
 
		this.add(cmdlimpiar);
		this.add(cmdsalir);
 
		this.add(chkopcion2D);
		this.add(chkopcion3D);
		this.add(chkopcion4D);
		this.add(chkopcionIMAX);
 
		combo.addItemListener(this);
		cmdsalir.addItemListener(this);
		this.setVisible(true);
	}
public void itemStateChanged(ItemEvent e){
 
    if (combo.getSelectedItem().equals("Batman The Dark Knight Rises")){
            jtxtgenero.setText("Accion");
            jtxtduracion.setText("165 min");
			jtxtclasificacion.setText("B");
		}
	else
	if (combo.getSelectedItem().equals("Transformers The Dark Side Of The Moon")){
		    jtxtgenero.setText("Ciencia Ficcion");
            jtxtduracion.setText("155 min");
			jtxtclasificacion.setText("A");
	}
	else
	if (combo.getSelectedItem().equals("Fast & Furious 6")){
		    jtxtgenero.setText("Accion");
            jtxtduracion.setText("90 min");
			jtxtclasificacion.setText("B");
	}
 
 
	}
 
    public static void main(String[] args) {
 
    Peliculas P=new Peliculas();
 
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Jhonnathan Emilio Cardona Saineda

Como utilizar varios Action Listener y ItemListener en el mismo JFrame

Publicado por Jhonnathan Emilio Cardona Saineda (328 intervenciones) el 22/11/2013 18:47:16
Hola,
1ero implementas la interfaz ActionListener eso te obligara a tener un metodo actionPerformed y a cada boton le añades lo que ya haces con el combo, el listener que sería tu propia clase. cmdlimpiar.addActionListener(this)
Ya en el metodo preguntas que boton hizo el evento.
Saludos,
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar