Está en la página 1de 14

UNIVERSIDAD TECNOLOGICA DE HONDURAS

CATEDRATICO: CARLOS HUGO ESTRADA PINEDA

ASIGNATURA: PROGRAMACION AVANZADA

PARCIAL: I PARCIAL

TEMA: EJERCICIOS DE LA EXPOSICION DE


JCOMBOBOX Y JLIST
INTRODUCCION

A continuación se adjunta los ejercicios realizados en la exposición


de los temas de JCOMBOBOX Y JLIST compilados y programados
por los integrantes del grupo 3.

pág. 1
Contenido
INTRODUCCION..............................................................................1
Ejercicios con el comando JCOMBOBOX.........................................3
Imágenes al ejecutar el comando JCOMBOBOX..............................6
Ejercicios con el comando JLIST......................................................7
Imágenes al ejecutar el comando JLIST........................................10
Ejercicio Investigado en internet para la presentación de los
comandos........................................................................................11
Imágenes al ejecutar el ejercicio investigado de internet................13

pág. 2
Ejercicios con el comando JCOMBOBOX

Codigo:

// Fig. 12.21: MarcoCuadroCombinado.java

// Objeto JComboBox que muestra una lista de nombres de


imágenes.

1. import java.awt.FlowLayout;
2. import java.awt.event.ItemListener;
3. import java.awt.event.ItemEvent;
4. import javax.swing.JFrame;
5. import javax.swing.JLabel;
6. import javax.swing.JComboBox;
7. import javax.swing.Icon;
8. import javax.swing.ImageIcon;

9. public class MarcoCuadroCombinado extends JFrame


10. {
11. private final JComboBox<String>
imagenesJComboBox; // contiene los nombres de los iconos
12. private final JLabel etiqueta; // muestra el icono
seleccionado

13. private static final String nombres[] =


14. {"fotoinsecto1.jfif", "fotoinsecto2.jfif", "fotoinsecto3.jfif",
"fotoinsecto4.jfif"};
15. private final Icon[] iconos = {
16. new ImageIcon(getClass().getResource(nombres[0])),
17. new ImageIcon(getClass().getResource(nombres[1])),
18. new ImageIcon(getClass().getResource(nombres[2])),
19. new ImageIcon(getClass().getResource(nombres[3]))};

pág. 3
20. // El constructor de MarcoCuadroCombinado agrega un
objeto JComboBox a JFrame
21. public MarcoCuadroCombinado()
22. {
23. super("Prueba de JComboBox");
24. setLayout(new FlowLayout()); // establece el esquema
del marco

25. imagenesJComboBox = new


JComboBox<String>(nombres); // establece JComboBox
26. imagenesJComboBox.setMaximumRowCount(3); //
muestra tres filas

27. imagenesJComboBox.addItemListener(
28. new ItemListener() // clase interna anónima
29. {
30. // maneja evento de JComboBox
31. @Override
32. public void itemStateChanged(ItemEvent evento)
33. {
34. // determina si está seleccionado el elemento
35. if (evento.getStateChange() == ItemEvent.SELECTED)
36. etiqueta.setIcon(iconos[
37. imagenesJComboBox.getSelectedIndex()]);
38. }
39. } // fin de la clase interna anónima
40. ); // fin de la llamada a addItemListener
41. add(imagenesJComboBox); // agrega cuadro combinado
a JFrame
42. etiqueta = new JLabel(iconos[0]); // muestra el primer
icono
43. add(etiqueta); // agrega etiqueta a JFrame
44. }
45. } // fin de la clase MarcoCuadroCombinado

/// Fig. 12.22: PruebaCuadroCombinado.java


// Prueba de MarcoCuadroCombinado.

pág. 4
1. import javax.swing.JFrame;

2. public class PruebaCuadroCombinado


3. {
4. public static void main(String[] args)
5. {
6. MarcoCuadroCombinado marcoCuadroCombinado = new
MarcoCuadroCombinado();
7. marcoCuadroCombinado.setDefaultCloseOperation(JFrame.E
XIT_ON_CLOSE);
8. marcoCuadroCombinado.setSize(390, 390);
9. marcoCuadroCombinado.setVisible(true);
10. }
11. } // fin de la clase PruebaCuadroCombinado

pág. 5
Imágenes al ejecutar el comando JCOMBOBOX

pág. 6
Ejercicios con el comando JLIST

Codigo

// Fig. 12.23: MarcoLista.java


// Objeto JList que muestra una lista de colores.
//Grupo #3
import java.awt.FlowLayout;

1. import java.awt.Color;
2. import javax.swing.JFrame;
3. import javax.swing.JList;
4. import javax.swing.JScrollPane;
5. import javax.swing.event.ListSelectionListener;
6. import javax.swing.event.ListSelectionEvent;
7. import javax.swing.ListSelectionModel;

8. public class MarcoLista extends JFrame


9. {
10. private final JList<String> listaJListColores; // lista para
mostrar colores
11. private static final String[] nombresColores = {"Negro",
"Azul", "Cyan",
12. "Gris oscuro", "Gris", "Verde", "Gris clar", "Magenta",
13. "Naranja", "Rosa", "Rojo", "Blanco", "Amarillo"};
14. private static final Color[] colores = {Color.BLACK,
Color.BLUE,
15. Color.CYAN, Color.DARK_GRAY, Color.GRAY,
Color.GREEN,
16. Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
Color.PINK,
17. Color.RED, Color.WHITE, Color.YELLOW};

pág. 7
18. // El constructor de MarcoLista agrega a JFrame el
JScrollPane que contiene a JList
19. public MarcoLista()
20. {
21. super("Prueba de JList");
22. setLayout(new FlowLayout());

23. listaJListColores = new


JList<String>(nombresColores); // lista de nombresColores
24. listaJListColores.setVisibleRowCount(5); // muestra
cinco filas a la vez

25. // no permite selecciones múltiples


26. listaJListColores.setSelectionMode(ListSelectionModel.S
INGLE_SELECTION);

27. // agrega al marco un objeto JScrollPane que contiene a


JList
28. add(new JScrollPane(listaJListColores));

29. listaJListColores.addListSelectionListener(
30. new ListSelectionListener() // clase interna anónima
31. {
32. // maneja los eventos de selección de la lista
33. @Override
34. public void valueChanged(ListSelectionEvent evento)
35. {
36. getContentPane().setBackground(
37. colores[listaJListColores.getSelectedIndex()]);
38. }
39. }
40. );
41. }

pág. 8
42. } // fin de la clase MarcoLista

// Fig. 12.24: PruebaLista.java


// Selección de colores de un objeto JList.
1. import javax.swing.JFrame;
2. public class PruebaLista
3. {
4. public static void main(String[] args)
5. {
6. MarcoLista marcoLista = new MarcoLista(); // crea objeto
MarcoLista
7. marcoLista.setDefaultCloseOperation(JFrame.EXIT_ON_CLO
SE);
8. marcoLista.setSize(400, 400);
9. marcoLista.setVisible(true);
10. }
11. } // fin de la clase PruebaLista

pág. 9
Imágenes al ejecutar el comando JLIST

pág. 10
Ejercicio Investigado en internet para la
presentación de los comandos

1. // java Program to create a list and add itemListener to it


2. // (program to select your birthday using lists).
3. import javax.swing.event.*;
4. import java.awt.*;
5. import javax.swing.*;
6.  
7. class solve extends JFrame implements ListSelectionListener {
8.
9. //frame
10. static JFrame f;
11.
12. //lists
13. static JList b,b1,b2;
14.
15. //label
16. static JLabel l1;
17.  
18.  
19. //main class
20. public static void main(String[] args) {
21. //create a new frame
22. f = new JFrame("frame");
23.
24. //create a object
25. solve s=new solve();
26.
27. //create a panel
28. JPanel p =new JPanel();
29.
30. //create a new label
31. JLabel l= new JLabel("select your birthday");
32. l1= new JLabel();
33.  
34. //String array to store weekdays
35. String month[]= { "January", "February", "March",
36. "April", "May", "June", "July", "August",
37. "September", "October", "November", "December"};
38.
39. //create a array for months and year
40. String date[]=new String[31],year[]=new String[31];
41.
42. //add month number and year to list
43. for(int i=0;i<31;i++) {
44. date[i]=""+(int)(i+1);
45. year[i]=""+(int)(2022-i);
46. }
47.
48. //create lists
49. b= new JList(date);
50. b1= new JList(month);
51. b2= new JList(year);
52.
53. //set a selected index
54. b.setSelectedIndex(2);
55. b1.setSelectedIndex(1);
56. b2.setSelectedIndex(2);

pág. 11
57.
58. l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()
59. +"
"+b2.getSelectedValue());
60.
61. //add item listener
62. b.addListSelectionListener(s);
63. b1.addListSelectionListener(s);
64. b2.addListSelectionListener(s);
65.
66. //add list to panel
67. p.add(l);
68. p.add(b);
69. p.add(b1);
70. p.add(b2);
71. p.add(l1);
72.  
73. f.add(p);
74.
75. //set the size of frame
76. f.setSize(700,700);
77.
78. f.show();
79. }
80. public void valueChanged(ListSelectionEvent e) {
81. //set the text of the label to the selected value of lists
82. l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()
83. +"
"+b2.getSelectedValue());
84.
85. }
86. }

pág. 12
Imágenes al ejecutar el ejercicio investigado de
internet

pág. 13

También podría gustarte