Sei sulla pagina 1di 6

19/2/2019 1.10.

4 La interfaz ItemListener

Página Principal (home) ► Educación Presencial ► Licenciatura ►


Ingeniería en Sistemas Computacionales ► TAPGR ► Unidad 1 ► 1.10.4 La interfaz ItemListener

http://eddi.ittepic.edu.mx/mod/quiz/attempt.php?attempt=163269 1/6
19/2/2019 1.10.4 La interfaz ItemListener
Pregunta 1
1 /*
Sin responder aún 2 Elabora un programa que contenga un ComboBox, con las palabras: Azul,
Puntaje de 16.00 Rojo, Verde, Amarillo y Rosa.
3 El color de la ventana deberá cambiar de color cuando se seleccione
alguna de los elementos
4 del ComboBox.
5 */
6
7 import javax.swing.JFrame;
8 import javax.swing.JComboBox;
9 import javax.swing.JLabel;
10 import java.awt.FlowLayout;
11 import java.awt.Color;

12 import java.awt.event. ItemListener ;

13 import java . awt . event .ItemEvent;

14
15 class Ejercicio_1_10_4_1 extends JFrame{
16
17 ////////////////////////////////////////////////////
18 public Ejercicio_1_10_4_1(){
19 inicializarComponentes();
20 }
21
22 ////////////////////////////////////////////////////
23 private void inicializarComponentes(){
24 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
25 this.setSize(200, 200);
26 this.setLayout(new FlowLayout());
27
28 final String[] listaNombres = {"Azul", "Rojo", "Verde",
"Amarillo", "Rosa"};
29 final Color[] listaColores = {Color.BLUE, Color.RED,
Color.GREEN, Color.YELLOW, Color.PINK};
30

31 JComboBox <String> combo = new JComboBox<String>(

listaNombres );

32 this.add(combo);
33

34 combo. addItemListener (new ItemListener (){

35 @ Override

36 public void itemStateChanged ( ItemEvent evento){

37 int índice = combo. getSelectedIndex() ();


http://eddi.ittepic.edu.mx/mod/quiz/attempt.php?attempt=163269 2/6
19/2/2019 1.10.4 La interfaz ItemListener
38 getContentPane(). setBackground ( listaColores

[índice]);
39 }
40
41 });
42
43 combo.setSelectedIndex(1);
44 }
45
46 ////////////////////////////////////////////////////
47 public static void main(String args[]){
48 new Ejercicio_1_10_4_1().setVisible(true);
49 }
50 }

http://eddi.ittepic.edu.mx/mod/quiz/attempt.php?attempt=163269 3/6
19/2/2019 1.10.4 La interfaz ItemListener
Pregunta 2
1 /*
Sin responder aún 2 Elabora un programa que contenga un campo de texto y dos
Puntaje de 27.00 "checkboxes", uno para cambiar el formato
3 de la fuente del campo de texto a cursiva y otro para cambiar
el formato de la fuente a negrita.
4 Para poder realizar este ejercicio, deberás de invetigar el
método setFont() de JTextField, y el
5 objeto Font de java.
6 */
7
8 import javax.swing.JFrame;
9 import javax.swing.JCheckBox;
10 import java.awt.FlowLayout;
11 import javax.swing.JTextField;
12 import java.awt.Font;
13 import java.awt. event . ItemListener ;

14 import java . awt . event .ItemEvent;

15
16 class Ejercicio_1_10_4_2 extends JFrame{
17
18 private JTextField texto;
19 private JCheckBox cursiva;
20 private JCheckBox negrita;
21
22 ////////////////////////////////////////////////////
23 public Ejercicio_1_10_4_2(){
24 inicializarComponentes();
25 }
26
27 ////////////////////////////////////////////////////
28 private void inicializarComponentes(){
29 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
30 this.setSize(200, 200);
31 this.setLayout(new FlowLayout());
32
33 texto = new JTextField (12);

34 texto.setText("Eventos con Java");


35 this.add( texto );

36
37 cursiva = new JCheckBox ("Cursiva");

38 this.add( cursiva );

39
40 negrita = new JCheckBox ("Negrita");
http://eddi.ittepic.edu.mx/mod/quiz/attempt.php?attempt=163269 4/6
19/2/2019 1.10.4 La interfaz ItemListener
41 this.add( negrita );

42
43 cursiva. addItemListener (new ItemListener (){

44 @Override
45 public void itemStateChanged ( ItemEvent evento)

{
46 cambiarFuente();
47 }
48
49 });
50
51 negrita . addItemListener (new ItemListener (){

52 @Override
53 public void itemStateChanged ( ItemEvent

evento){
54 cambiarFuente();
55 }
56
57 });
58 }
59
60 ////////////////////////////////////////////////////
61 private void cambiarFuente(){
62 int c, n;
63
64 //Cambiamos el valor de c dependiendo si está
65 //seleccionado o no el checkbox de cursiva
66 if (cursiva. isSelected() ()){

67 c = Font. ;

68 }else{
69 c = Font. ;

70 }
71
72 //Cambiamos el valor de n dependiendo si está
73 //seleccionado o no el checkbox de negrita
74 if (negrita. isSelected() ()){

75 n = Font. BOLD| ;

76 }else{
77 n = Font. ;

78 }
http://eddi.ittepic.edu.mx/mod/quiz/attempt.php?attempt=163269 5/6
19/2/2019 79 1.10.4 La interfaz ItemListener

80 //Ponemos la fuente del textfield, pasando


81 //los valores de las variables n y c
82 texto. setFont (new Font("Arial", n | c, 12));

83 }
84
85 ////////////////////////////////////////////////////
86 public static void main(String args[]){
87 new Ejercicio_1_10_4_2().setVisible(true);
88 }
89 }

http://eddi.ittepic.edu.mx/mod/quiz/attempt.php?attempt=163269 6/6

Potrebbero piacerti anche